我正在使用 Espresso 开发一个测试来测试个人资料图像更改功能。我在测试的@Before 方法中添加了以下行。
我使用图像 Uri 和我的文件提供程序创建一个 Intent,以返回我的应用程序去图库选择图像的任何时间。
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);
更改用户图像的 Activity 代码,当它收到 Intent 时调用以下方法,(我不能更改它)。
mProfileImage = CommonBitmapUtils.rotate(this, data.getData());
profileEdited = true;
imgUserPhoto.setImageBitmap(mProfileImage);
我收到以下错误:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
由 CommonBitmapUtils 类的函数旋转中的这一行引起:
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
游标有0行不知道为什么。
最佳答案
通过设置以下路径解决了它。没有文件提供者和权限。
我通过未经测试调试应用程序并复制值来获得路径。
resultData.setData(Uri.parse("content://media/external/images/media/142583"));
关于java - Espresso 图像选择器 - CursorIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044760/