java - Espresso 图像选择器 - CursorIndexOutOfBoundsException 错误
问题描述
我正在用 Espresso 开发一个测试来测试个人资料图像更改功能。我在测试的@Before 方法中添加了以下几行。
我使用图像 Uri 和我的文件提供程序创建了一个意图,以返回我的应用程序进入图库以选择图像。
Intent resultData = new Intent();
String filename = "img1.jpg";
String path = "mnt/sdcard/" + filename;
File f = new File(path);
Context context =InstrumentationRegistry.getInstrumentation().getContext();
Uri contentUri = getUriForFile(context, "com.otsuka.ikigai.fileprovider", f);
resultData.setData(contentUri);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK,resultData);
intending(not(isInternal())).respondWith(result);
更改用户图像的活动代码,在收到意图时调用以下方法,(我不能更改它)。
mProfileImage = CommonBitmapUtils.rotate(this, data.getData());
profileEdited = true;
imgUserPhoto.setImageBitmap(mProfileImage);
我收到以下错误:
android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0
由 CommonBitmapUtils 类的函数 rotate 中的这一行引起:
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
游标有 0 行不知道为什么。
解决方案
通过设置以下路径解决了它。没有文件提供者和权限。
我通过在没有测试的情况下调试应用程序并复制值来获得路径。
resultData.setData(Uri.parse("content://media/external/images/media/142583"));
推荐阅读
- javascript - 更改图像映射加载的视频属性
- dart - 使用 Navigator.pop() 颤振“showDialog”
- google-app-engine - 无需添加 DNS 条目即可访问 GHS 应用引擎
- yii - Yii2 - hasMany 与多列的关系
- angular - 在 auth 用户中遇到问题“第一个参数“电子邮件”必须是有效字符串”
- handlebars.js - 屏幕阅读器未阅读工具提示气泡消息
- c++ - 我的输出和矩阵填充 -nan 而不是 0 并且不知道为什么
- php - 在 Slim Framework 路由中按关键字搜索
- android - Android设置活动未导航回逻辑父项
- javascript - 返回 SweetAlert2 电子邮件值