简体   繁体   English

Cordova Android 应用程序在捕获照片时崩溃并出现 RecoverableSecurityException

[英]Cordova Android app crashes with RecoverableSecurityException while capturing photo

Cordova android app crashing frequently while capturing photo using camera. Cordova android 应用程序在使用相机拍摄照片时经常崩溃。 The stacktrace from crashlytics is crashlytics 的堆栈跟踪是

Caused by android.app.RecoverableSecurityException: com.myapp.appname has no access to content://media/external/images/media/131613
   at android.app.RecoverableSecurityException$1.createFromParcel(RecoverableSecurityException.java:197)
   at android.app.RecoverableSecurityException$1.createFromParcel(RecoverableSecurityException.java:194)
   at android.os.Parcel.readParcelable(Parcel.java:3295)
   at android.os.Parcel.createExceptionOrNull(Parcel.java:2382)
   at android.os.Parcel.createException(Parcel.java:2371)
   at android.os.Parcel.readException(Parcel.java:2354)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
   at android.content.ContentProviderProxy.delete(ContentProviderNative.java:624)
   at android.content.ContentResolver.delete(ContentResolver.java:2386)
   at android.content.ContentResolver.delete(ContentResolver.java:2344)
   at org.apache.cordova.camera.CameraLauncher.checkForDuplicateImage(CameraLauncher.java:1234)
   at org.apache.cordova.camera.CameraLauncher.cleanup(CameraLauncher.java:1200)
   at org.apache.cordova.camera.CameraLauncher.processResultFromCamera(CameraLauncher.java:597)
   at org.apache.cordova.camera.CameraLauncher.onActivityResult(CameraLauncher.java:829)
   at org.apache.cordova.CordovaInterfaceImpl.onActivityResult(CordovaInterfaceImpl.java:160)
   at org.apache.cordova.CordovaActivity.onActivityResult(CordovaActivity.java:368)
   at android.app.Activity.dispatchActivityResult(Activity.java:8627)
   at android.app.ActivityThread.deliverResults(ActivityThread.java:5466)
   at android.app.ActivityThread.handleSendResult(ActivityThread.java:5514)
   at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2380)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:257)
   at android.app.ActivityThread.main(ActivityThread.java:8335)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1032)

Most of the crashes happened in Samsung devices with Android 11 and 12.大多数崩溃发生在配备 Android 11 和 12 的三星设备上。

The code for capturing the photo is拍摄照片的代码是

    navigator.camera.getPicture(onPhotoDataSuccess, onPhotoCaptureFail, {
        quality: 100,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: 1,
        encodingType: 0,
        allowEdit: false,
        targetWidth: 1024,
        targetHeight: 768,
        correctOrientation: true
    });

I have tried to resolve this by adding try catch inside the Camera plugin's CameraLauncher.java file as suggested here but camera is not opening after that我已尝试通过在相机插件的 CameraLauncher.java 文件中添加 try catch 来解决此问题,如此处所建议但此后相机未打开

private void checkForDuplicateImage(int type) {
int diff = 1;
Uri contentStore = whichContentStore();
Cursor cursor = queryImgDB(contentStore);
int currentNumOfImages = cursor.getCount();

if (type == FILE_URI && this.saveToPhotoAlbum) {
  diff = 2;
}

// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) == diff) {
  cursor.moveToLast();
  int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
  if (diff == 2) {
    id--;
  }
  Uri uri = Uri.parse(contentStore + "/" + id);
  ContentResolver contentResolver = this.cordova.getActivity().getContentResolver();

  try {
    contentResolver.delete(uri, null, null);
  } catch (SecurityException e) {
    if (this.manualMediaStoreCleanup && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

      ArrayList<Uri> collection = new ArrayList<>();
      collection.add(uri);
      PendingIntent pendingIntent = MediaStore.createDeleteRequest(contentResolver, collection);
      try {
        this.cordova.getActivity().startIntentSenderForResult(pendingIntent.getIntentSender(),
          42, null, 0, 0,
          0, null);

      } catch (IntentSender.SendIntentException ex) {
        LOG.e(LOG_TAG, "Error starting deleteRequest intent");
      }

    } else if (this.manualMediaStoreCleanup && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

      //if exception is recoverable then again send delete request using intent
      if (e instanceof RecoverableSecurityException) {
        RecoverableSecurityException exception = (RecoverableSecurityException) e;
        PendingIntent pendingIntent = exception.getUserAction().getActionIntent();

        try {
          mediaStoreUri = uri;
         this.cordova.setActivityResultCallback(this);
          this.cordova.getActivity().startIntentSenderForResult(pendingIntent.getIntentSender(),
            RECOVERABLE_DELETE_REQUEST, null, 0, 0,
            0, null);
        } catch (IntentSender.SendIntentException ex) {
          LOG.e(LOG_TAG, "Error starting RecoverableSecurityException intent");
        }
      }
    } else {
      LOG.e(LOG_TAG, "Error during media store file deletion");    
    }

  }

  cursor.close();
}

} }

Did anyone faced this issue?有人遇到过这个问题吗? Any help would be really appreciated任何帮助将非常感激

Versions used are:-使用的版本是:-

  • Cordova: 10.0.0 (cordova-lib@10.1.0) Cordova:10.0.0(cordova-lib@10.1.0)
  • cordova-android: 10.1.1科尔多瓦机器人:10.1.1
  • cordova-plugin-camera: 6.0.0科尔多瓦插件相机:6.0.0
  • Device: Samsung devices with Android 11 or 12设备:带有 Android 11 或 12 的三星设备

I have the same issue on an Android 10 Device (Armor_X8).我在 Android 10 设备 (Armor_X8) 上遇到了同样的问题。 Everything is up to date.一切都是最新的。

"plugin_metadata": {
"cordova-plugin-app-version": "0.1.12",
"cordova-plugin-device": "2.0.3",
"cordova-plugin-geolocation": "4.1.0",
"es6-promise-plugin": "4.2.2",
"cordova-plugin-screen-orientation": "3.0.2",
"cordova-plugin-inappbrowser": "5.0.0",
"@red-mobile/cordova-plugin-barcodescanner": "9.0.7",
"cordova-plugin-network-information": "3.0.0",
"cordova-plugin-camera": "6.0.0"

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM