Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to check if storage permission is granted #287

Closed
TheLastGimbus opened this issue May 3, 2020 · 16 comments · Fixed by #1184
Closed

Add method to check if storage permission is granted #287

TheLastGimbus opened this issue May 3, 2020 · 16 comments · Fixed by #1184

Comments

@TheLastGimbus
Copy link
Contributor

We already have PhotoManager.requestPermission() for getting permission.

It would be nice to have method to check if permission is granted.

@TheLastGimbus
Copy link
Contributor Author

I wrote this wile writing some code that was using PhotoManager in the background (using workmanager

It keeps giving me exception:

E/MethodChannel#top.kikt/photo_manager( 5686): Failed to handle method call
E/MethodChannel#top.kikt/photo_manager( 5686): java.lang.NullPointerException: 获取权限的Activity不存在
E/MethodChannel#top.kikt/photo_manager( 5686): 	at top.kikt.imagescanner.permission.PermissionsUtils.getPermissionsWithTips(PermissionsUtils.java:110)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at top.kikt.imagescanner.permission.PermissionsUtils.getPermissions(PermissionsUtils.java:95)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at top.kikt.imagescanner.core.PhotoManagerPlugin.onMethodCall(PhotoManagerPlugin.kt:152)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at android.app.ActivityThread.main(ActivityThread.java:6718)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
E/MethodChannel#top.kikt/photo_manager( 5686): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

At first, I thought that it's because I don't have a permission

java.lang.NullPointerException: 获取权限的Activity不存在

But it looks like you specifically use some activity when checking for permission.
It needs to be fixed to work even when no activity is present.

@CaiJingLong
Copy link
Member

Unfortunately, you must have an Activity instance to apply for runtime permissions.
WorkManager cannot provide an Activity instance, so you cannot use the library in the background.

@TheLastGimbus
Copy link
Contributor Author

TheLastGimbus commented Sep 2, 2020

I just tested this with workmanager and permission_handler plugins. Permission handler can get permission status in background, without any activity, even in "recent apps" list, without any problem.

If this is just a thing of checking permission, we can just use permission_handler plugin, and everything will work!

@alejandrogiubel
Copy link

I have the same error. I am using workmanager and PhotoManager and I need to process some photos in background and I have this error

E/MethodChannel#top.kikt/photo_manager(16516): Failed to handle method call
E/MethodChannel#top.kikt/photo_manager(16516): java.lang.NullPointerException: 获取权限的Activity不存在
E/MethodChannel#top.kikt/photo_manager(16516): 	at top.kikt.imagescanner.permission.PermissionsUtils.getPermissionsWithTips(PermissionsUtils.java:110)
E/MethodChannel#top.kikt/photo_manager(16516): 	at top.kikt.imagescanner.permission.PermissionsUtils.getPermissions(PermissionsUtils.java:95)
E/MethodChannel#top.kikt/photo_manager(16516): 	at top.kikt.imagescanner.core.PhotoManagerPlugin.onMethodCall(PhotoManagerPlugin.kt:157)
E/MethodChannel#top.kikt/photo_manager(16516): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
E/MethodChannel#top.kikt/photo_manager(16516): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#top.kikt/photo_manager(16516): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/MethodChannel#top.kikt/photo_manager(16516): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#top.kikt/photo_manager(16516): 	at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#top.kikt/photo_manager(16516): 	at android.os.Looper.loop(Looper.java:197)
E/MethodChannel#top.kikt/photo_manager(16516): 	at android.app.ActivityThread.main(ActivityThread.java:7811)
E/MethodChannel#top.kikt/photo_manager(16516): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#top.kikt/photo_manager(16516): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#top.kikt/photo_manager(16516): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
I/WM-WorkerWrapper(16516): Worker result FAILURE for Work [ id=fd0036ed-1aee-4afa-b57b-2f6a423aa559, tags={ be.tramckrijte.workmanager.BackgroundWorker } ]

And this is my code

Future<void> deleteUnusedImageFromDataBase() async {
  print('Start deleteUnusedImageFromDataBase');
  bool permission = await Permission.storage.isGranted;
  print(permission);
  if (permission) {
    List<AssetPathEntity> albums = await PhotoManager.getAssetPathList(onlyAll: true, type: RequestType.image);
    List<AssetEntity> media = await albums[0].assetList;
    print('works');
    var box = Hive.box(imagesBox);
    box.toMap().forEach((key, value) {
      ProcessedImage pi = value as ProcessedImage;
      bool exist = false;
      for (int i = 0; i < media.length; i++) {
        if (pi.id == media[i].id) {
          exist = true;
          break;
        }
      }
      if (!exist) {
        pi.delete();
      }
    });
    print('Finish deleteUnusedImageFromDataBase');
  }
}

Is there any way to use PhotoManager in background?

@alejandrogiubel
Copy link

I resolved the issue using PhotoManager.setIgnorePermissionCheck(true);. Thank.

@TheLastGimbus
Copy link
Contributor Author

Do other functions work? Like, actually getting images?

@alejandrogiubel
Copy link

@TheLastGimbus I use PhotoManager.getAssetPathList(onlyAll: true, type: RequestType.image) in background process with workmanager. This is the only function that I use in workmanager.

@TheLastGimbus
Copy link
Contributor Author

And... it works? Without the app in the foreground whatsoever?

Also, I'm not recently in touch with my project that used this library - I would be really grateful if you could quickly check if some other functions work 🙏❤️

@alejandrogiubel
Copy link

Yes. So far it works. I process images in the background using those two libraries. With the application in the foreground, the process works completely to finish but when I close the application the process only take approximately 55 seconds and them is cancelled, but I think it was a change in the code that I made. Right now I don't have my code at my disposal but as soon as I can I can verify what you ask me. 👍

@alejandrogiubel
Copy link

Ok. I check if other function work and... (drumroll)... it works.
I use this method that delete an image with ID = 20873 and execute it (in background) when I close the app.

backGroundTest() {
  PhotoManager.editor.deleteWithIds(['20873']);
}

@danielseow-simplify
Copy link

danielseow-simplify commented Mar 15, 2021

I resolved the issue using PhotoManager.setIgnorePermissionCheck(true);. Thank.

@alejandrogiubel I've tried doing like this, is this the right way?

await PhotoManager.setIgnorePermissionCheck(true);
List<AssetPathEntity> list = await PhotoManager.getAssetPathList(type: RequestType.image);

but it still prompt me this permission dialog, how can I stop this dialog from showing every time when user only choose selected photos to be accessed?
image

@TheLastGimbus
Copy link
Contributor Author

Maybe you didn't await ?

@danielseow-simplify
Copy link

@TheLastGimbus Oops, sorry I accidentally removed it when I was doing screenshot, actually I was having the same issue with await as well

@AlexV525
Copy link
Member

AlexV525 commented Oct 4, 2021

Permissions would be dynamic checking things, so I guess the most proper way is to use the request method instead?

@AlexV525
Copy link
Member

AlexV525 commented Oct 4, 2021

There are multiple requests/features involved in the whole discussion, if anyone have questions other than the new method, please submit new issues instead of leaving new comments here.

@Kal-Elx
Copy link

Kal-Elx commented Oct 13, 2021

I resolved the issue using PhotoManager.setIgnorePermissionCheck(true);. Thank.

@alejandrogiubel I've tried doing like this, is this the right way?

await PhotoManager.setIgnorePermissionCheck(true);
List<AssetPathEntity> list = await PhotoManager.getAssetPathList(type: RequestType.image);

but it still prompt me this permission dialog, how can I stop this dialog from showing every time when user only choose selected photos to be accessed? image

If anyone is finding this issue because they have the same problem as @danielseow-simplify the solution is to add PHPhotoLibraryPreventAutomaticLimitedAccessAlert: YES to info.plist as suggested in this article.

@AlexV525 AlexV525 linked a pull request Sep 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants