dragermrb / capacitor-plugin-video-editor

Capacitor plugin to edit videos
10 stars 8 forks source link

Android: Permission Error in 5.x when Generating Thumbnails #9

Closed AdlerJS closed 1 year ago

AdlerJS commented 1 year ago

When trying to use the thumbnail function on Capacitor Android to generate a thumbnail preview I'm getting the following error thrown: "User denied access to storage"

Note: I've also tried adding the following permissions:

uses-permission android:name="android.permission.READ_MEDIA_VIDEO" Android Version: ~ Android 13 (API level 33)

Code:

        if (uri && size) {
          try {
            const { file } = await VideoEditor.thumbnail({
              path: normalizeVideoPath(uri),
              width: 600,
              height: 600,
              at: 0,
            });

            setFileThumbnail(Capacitor.convertFileSrc(file.path));
          } catch (error) {
            console.log('THUMB ERROR**', error);
            appInsights.trackException({
              exception: error,
              severityLevel: SeverityLevel.Error,
              properties: { name: 'thumbnail creation', fileId },
            });
          }
        }

I have what I believe is the correct permissions in AndroidManifest.xml

   <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" />
    <uses-permission android:name="android.permission.USE_BIOMETRIC" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="com.android.vending.BILLING"/>
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

Logcat Info:

callback: 63712670, pluginId: VideoEditor, methodName: thumbnail, methodData: {"path":"\/storage\/emulated\/0\/Android\/data\\/files\/2518e2d2-f53e-409e-ae59-3ea78f733038-PXL_20221102_142143045.mp4","width":600,"height":600,"at":0}
2023-07-25 14:16:19.351  8950-8950  Capacitor   D  Sending plugin error: {"save":false,"callbackId":"63712670","pluginId":"VideoEditor","methodName":"thumbnail","success":false,"error":{"message":"User denied access to storage"}}
AdlerJS commented 1 year ago

@dragermrb - Did want to give more info after doing a bit of research. I think this is related specifically to the Android code

CapacitorPlugin(
        name = "VideoEditor",
        permissions = {
                @Permission(
                        strings = {Manifest.permission.READ_EXTERNAL_STORAGE},
                        alias = VideoEditorPlugin.STORAGE
                )
        }
)

On Android 13 - was deprecated (see: https://medium.com/@maydin/how-to-migrate-read-external-storage-permission-to-read-media-images-permission-for-android-13-ab99de39318) meaning if you are targeting SDK 33 on Android 13 the READ_EXTERNAL_STORAGE dialog request will be ignored at the native level. My guess here is that this plugin is expecting that permission. Instead we need to target the new permissions similar to this example from the medium article:

private val readImagePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) Manifest.permission.READ_MEDIA_IMAGES else Manifest.permission.READ_EXTERNAL_STORAGE
if(ContextCompat.checkSelfPermission(this, readImagePermission) == PackageManager.PERMISSION_GRANTED){
    //permission granted
} else {
    //permission not granted
}

Just to be explicit the new permissions are as follows: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO and READ_MEDIA_AUDIO.

Let me know if there is anymore I can do or add to help. Thanks!

dragermrb commented 1 year ago

Hi Dan

I'm on vacation for two weeks, I'll look at it when I get back

El mar, 25 jul 2023, 21:37, Dan Adler @.***> escribió:

@dragermrb https://github.com/dragermrb - Did want to give more info after doing a bit of research. I think this is related specifically to the Android code

CapacitorPlugin( name = "VideoEditor", permissions = { @Permission( strings = {Manifest.permission.READ_EXTERNAL_STORAGE}, alias = VideoEditorPlugin.STORAGE ) } )

On Android 13 - was deprecated (see: @.***/how-to-migrate-read-external-storage-permission-to-read-media-images-permission-for-android-13-ab99de39318) meaning if you are targeting SDK 33 on Android 13 the READ_EXTERNAL_STORAGE dialog request will be ignored at the native level. My guess here is that this plugin is expecting that permission. Instead we need to target the new permissions similar to this example from the medium article:

private val readImagePermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) Manifest.permission.READ_MEDIA_IMAGES else Manifest.permission.READ_EXTERNAL_STORAGE if(ContextCompat.checkSelfPermission(this, readImagePermission) == PackageManager.PERMISSION_GRANTED){ //permission granted } else { //permission not granted }

Just to be explicit the new permissions are as follows: READ_MEDIA_IMAGES https://developer.android.com/reference/android/Manifest.permission#READ_MEDIA_IMAGES, READ_MEDIA_VIDEO https://developer.android.com/reference/android/Manifest.permission#READ_MEDIA_VIDEO and READ_MEDIA_AUDIO https://developer.android.com/reference/android/Manifest.permission#READ_MEDIA_AUDIO .

Let me know if there is anymore I can do or add to help. Thanks!

— Reply to this email directly, view it on GitHub https://github.com/dragermrb/capacitor-plugin-video-editor/issues/9#issuecomment-1650422709, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACXSVEGLLEIMUC77DYM3THTXSAN57ANCNFSM6AAAAAA2XO24XE . You are receiving this because you were mentioned.Message ID: @.***>

AdlerJS commented 1 year ago

@dragermrb - I know you are vacation and hope you enjoy it. I put up a PR to fix this issue here as it is a blocker for an upcoming release we have. If you wouldn't mind taking a look. Just trying to avoid having to point our app towards a local fork.

https://github.com/dragermrb/capacitor-plugin-video-editor/pull/12

Thanks!

dragermrb commented 1 year ago

Merged #12