0

我一直在努力解决在使用相机应用程序拍照时有时会破坏我的活动的问题,导致以下错误和应用程序崩溃。

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity 
{com.example.myapp/com.example.myapp.activities.MyActivity}
 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

我已经按照 Google 文档了解如何在此处使用相机拍照。经过 3 或 4 次试验,它仍然产生上述错误。

我放弃了startActivityForResult新的旧方法Activity Result APIs,但仍然存在同样的问题。

此处的文档具体说明了以下内容:

在为结果启动活动时,有可能(并且,在内存密集型操作的情况下,例如使用相机,几乎可以肯定)您的进程和活动将由于内存不足而被破坏。

出于这个原因,活动结果 API 将结果回调与代码中您启动其他活动的位置分离。由于重新创建流程和活动时结果回调需要可用,因此每次创建活动时都必须无条件注册回调,即使启动其他活动的逻辑仅基于用户输入或其他业务逻辑发生。

因此,它指出即使活动被销毁,新的活动结果 API 也会以某种方式处理这个问题。

这是我现在使用的代码:

public class MyActivity extends FragmentActivity{
private Uri photoURI;
private ImageView firstImage;
ActivityResultLauncher<Uri> intentActivityResultLauncher;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_activity);
        firstImage = findViewById(R.id.imageView_camera_open_one);
        intentActivityResultLauncher=registerForActivityResult(new ActivityResultContracts.TakePicture(),
                result -> {
         if (firstImage != null && photoURI != null)
                    firstImage.setImageURI(photoURI);
                });
}

private void openCamera(int viewId) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Toast.makeText(this, "Error).show();
            }

            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        intentActivityResultLauncher.launch(photoURI);
                }
            }
        }
    }

 private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = "JPEG" + timeStamp;
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName, ".jpg", storageDir);
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }
}

将活动添加android:configChanges="orientation|screenSize"或修复到肖像没有帮助。

对此有适当的解决方法吗?

完整的堆栈跟踪:

java.lang.RuntimeException: Unable to resume activity {com.example.myapp/com.example.myapp.activities.Activity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.myapp/com.example.myapp.activities.Activity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4626)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4659)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2261)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8107)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.myapp/com.example.myapp.activities.Activity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5324)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4613)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4659) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2261) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8107) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 
     Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at com.example.myapp.activities.Activity.onActivityResult(MyActivity.java:269)
        at android.app.Activity.dispatchActivityResult(Activity.java:8294)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5317)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4613) 
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4659) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2261) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8107) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
4

0 回答 0