我要设置从相机拍摄的图像的3个图像视图。从片段中触发了相机意图,正在捕获图片,但是当返回到应用片段时,它会被破坏并重新创建。
我将所有3个图像路径保存在列表中,但是由于片段被破坏,列表被重新初始化,列表的大小变为0。
触发相机的意图是:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (getActivity() != null) {
if(takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoPaths.add(photoFile.getAbsolutePath());
Uri photoURI=FileProvider.getUriForFile(getContext(),"com.example.parking.fileprovider",photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
然后在onActivityResult中获取图像意图:
Bitmap bitmap;
// here imgCount is for checking number of images captured since I only want to capture 3 images.
if (imgCount == 0) {
bitmap = cropImageForApp(preview1, photoPaths.get(imgCount));
preview1.setImageBitmap(bitmap);
imgCount++;
} else if (imgCount == 1) {
bitmap = cropImageForApp(preview2, photoPaths.get(imgCount));
preview2.setImageBitmap(bitmap);
imgCount++;
} else if(imgCount ==2) {
bitmap =cropImageForApp(preview3,photoPaths.get(imgCount));
preview3.setImageBitmap(bitmap);
imgCount++;
}
预期结果:要捕获的图像并将其设置在imageview上。
我得到的错误是:
java.lang.RuntimeException:无法恢复活动{com.parking.vehicleparking / com.parking.vehicleparking.activity.RegisterActivity}:java.lang.RuntimeException:无法传送结果ResultInfo {who = null,request = 65537, result = -1,data = null}到活动{com.parking.vehicleparking / com.parking.vehicleparking.activity.RegisterActivity}:java.lang.IndexOutOfBoundsException:索引:0,大小:0 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3657) 在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3697) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2927) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1639) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:176) 在android.app.ActivityThread.main(ActivityThread.java:6662) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 由以下原因引起:java.lang.RuntimeException:无法将结果ResultInfo {who = null,request = 65537,result = -1,data = null}传递到活动{com.parking.vehicleparking / com.parking.vehicleparking.activity.RegisterActivity} :java.lang.IndexOutOfBoundsException:索引:0,大小:0 在android.app.ActivityThread.deliverResults(ActivityThread.java:4365) 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3629) 在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3697) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2927) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1639) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:176) 在android.app.ActivityThread.main(ActivityThread.java:6662) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 引起原因:java.lang.IndexOutOfBoundsException:索引:0,大小:0 在java.util.ArrayList.get(ArrayList.java:437) 在com.parking.vehicleparking.fragment.CaptureImageFragment.onActivityResult(CaptureImageFragment.java:143) 在android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:160) 在com.parking.vehicleparking.activity.RegisterActivity.onActivityResult(RegisterActivity.java:275) 在android.app.Activity.dispatchActivityResult(Activity.java:7341) 在android.app.ActivityThread.deliverResults(ActivityThread.java:4361) 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3629) 在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3697) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2927) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1639) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:176) 在android.app.ActivityThread.main(ActivityThread.java:6662) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)