从启动特定 activity 启动时,startActivityForResult() 是否不起作用?

Does startActivityForResult() not work when initiated from launching a specific activity?

在我的启动选项中,我正在启动一个特定的 activity,在本例中它被称为“DailyActivity.java”。

在那个 class 中,我有一个按钮的 OnClicklistener,它应该打开相机意图拍照,然后对其执行文本提取。

fab_camera.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
 ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DISPLAY_NAME, "myimage.jpg");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
                values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
                imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
values);


                Intent camintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                camintent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                startActivityForResult(camintent, 101); }});

这里是 startActivityForResult 执行后 运行 应该执行的 onActivityResult

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Uri image2 = data.getData();
        InputImage image = null;
        try {
            image = InputImage.fromFilePath(this, image2);

            TextRecognizer recognizer = TextRecognition.getClient();

当我点击我的按钮时,我的应用程序崩溃了。我目前正在调试过程中,想知道这是否可能是因为我最初是从特定的 activity 启动的?如果我希望相机按钮稍后将我重定向到另一个 activity(文本提取中的第三个),这会影响未来的测试吗?

这是我在 logcat 中单击按钮时看到的错误

2020-12-04 23:19:54.986 25037-25064/com.example.smartcalendar E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2020-12-04 23:19:59.633 25037-25037/com.example.smartcalendar E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.smartcalendar, PID: 25037
    java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=25037, uid=10087 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
        at android.os.Parcel.createException(Parcel.java:1950)
        at android.os.Parcel.readException(Parcel.java:1918)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
        at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
        at android.content.ContentResolver.insert(ContentResolver.java:1587)
        at com.example.smartcalendar.DailyActivity.onClick(DailyActivity.java:128)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access00(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的 Android 清单文件的权限。

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在 android 6 或更高版本上,您必须在打开相机意图之前检查运行时中的权限。操作方法如下:

fab_camera.setOnClickListener{

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_DENIED ||
                checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_DENIED
            ) {
                // Permission was not granted
                // Ask for runtime permission
                val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                   
                    requestPermissions(
                        permissions,
                        PERMISSION_CODE
                    )

            } else {
                // Permission already granted
                openCamera()
            }

        } else {
            // System OS < Marshmallow
            openCamera()
        }
  }