活动结果 APITakePicture() 相机:未能提供结果 ResultInfo应用程序崩溃

问题描述

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

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 文档了解如何使用相机 here 拍照。试了三四次还是出现上述错误

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

文档 here,具体说明如下:

当开始一项活动以获得结果时,这是可能的(并且,在某些情况下 内存密集型操作,例如相机使用,几乎可以肯定) 您的流程和活动将因低 记忆。

因此,Activity Result API 将结果回调解耦 从代码中启动其他活动的位置。作为 结果回调需要在您的流程和 活动被重新创建,回调必须是无条件的 每次创建您的活动时都注册,即使逻辑 启动其他活动仅基于用户输入或其他 业务逻辑。

因此,它指出即使 Activity 被销毁,新的 Activity Result 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" 或将 Activity 修复为纵向,无济于事。

是否有适当的解决方法

完整的堆栈跟踪:

java.lang.RuntimeException: Unable to resume activity {com.example.myapp/com.example.myapp.activities.Activity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,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,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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...