首页 > 解决方案 > 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 行不知道为什么。

标签: javaandroidtestingandroid-espresso

解决方案


通过设置以下路径解决了它。没有文件提供者和权限。

我通过在没有测试的情况下调试应用程序并复制值来获得路径。

resultData.setData(Uri.parse("content://media/external/images/media/142583"));

推荐阅读