jachzen / cunning_document_scanner

MIT License
79 stars 83 forks source link

RuntimeException on some Android 11 #24

Closed vicajilau closed 1 year ago

vicajilau commented 1 year ago

First of all, a wonderful package.

With Android 11 in API 30. When I set the permissions to NEVER it throws an execution exception.

W/rreras.drag_pd(10713): Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/rreras.drag_pd(10713): Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/AndroidRuntime(10713): Shutting down VM
E/AndroidRuntime(10713): FATAL EXCEPTION: main
E/AndroidRuntime(10713): Process: es.victorcarreras.drag_pdf, PID: 10713
E/AndroidRuntime(10713): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3548984, result=-1, data=Intent { (has extras) }} to activity {es.victorcarreras.drag_pdf/es.victorcarreras.mell_pdf.MainActivity}: java.lang.Exception: error - error opening camera: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera2/com.android.camera.CaptureActivity clip={text/uri-list {...}} (has extras) } from ProcessRecord{1942f1b 10713:es.victorcarreras.drag_pdf/u0a168} (pid=10713, uid=10168) with revoked permission android.permission.CAMERA
E/AndroidRuntime(10713):    at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
E/AndroidRuntime(10713):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
E/AndroidRuntime(10713):    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
E/AndroidRuntime(10713):    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(10713):    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(10713):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
E/AndroidRuntime(10713):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(10713):    at android.os.Looper.loop(Looper.java:223)
E/AndroidRuntime(10713):    at android.app.ActivityThread.main(ActivityThread.java:7656)
E/AndroidRuntime(10713):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(10713):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/AndroidRuntime(10713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/AndroidRuntime(10713): Caused by: java.lang.Exception: error - error opening camera: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera2/com.android.camera.CaptureActivity clip={text/uri-list {...}} (has extras) } from ProcessRecord{1942f1b 10713:es.victorcarreras.drag_pdf/u0a168} (pid=10713, uid=10168) with revoked permission android.permission.CAMERA
E/AndroidRuntime(10713):    at biz.cunning.cunning_document_scanner.CunningDocumentScannerPlugin.addActivityResultListener$lambda$1(CunningDocumentScannerPlugin.kt:85)
E/AndroidRuntime(10713):    at biz.cunning.cunning_document_scanner.CunningDocumentScannerPlugin.$r8$lambda$tjh6xtgplCwJlLfhC6dUXO1duqo(Unknown Source:0)
E/AndroidRuntime(10713):    at biz.cunning.cunning_document_scanner.CunningDocumentScannerPlugin$$ExternalSyntheticLambda0.onActivityResult(Unknown Source:2)
E/AndroidRuntime(10713):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:809)
E/AndroidRuntime(10713):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:432)
E/AndroidRuntime(10713):    at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:872)
E/AndroidRuntime(10713):    at io.flutter.embedding.android.FlutterActivity.onActivityResult(FlutterActivity.java:917)
E/AndroidRuntime(10713):    at android.app.Activity.dispatchActivityResult(Activity.java:8304)
E/AndroidRuntime(10713):    at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
E/AndroidRuntime(10713):    ... 11 more
I/Process (10713): Sending signal. PID: 10713 SIG: 9
Lost connection to device.
vicajilau commented 1 year ago

I have found that PermissionStatus.permanentlyDenied is not handled in CunningDocumentScanner

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';

class CunningDocumentScanner {
  static const MethodChannel _channel =
      MethodChannel('cunning_document_scanner');

  /// Call this to start get Picture workflow.
  static Future<List<String>?> getPictures() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.camera,
    ].request();
    if (statuses.containsValue(PermissionStatus.denied) || statuses.containsValue(PermissionStatus.permanentlyDenied)) {
      throw Exception("Permission not granted");
    }

    final List<dynamic>? pictures = await _channel.invokeMethod('getPictures');
    return pictures?.map((e) => e as String).toList();
  }
}

I will do a PR with this change. Greetings!!

jachzen commented 1 year ago

Done