ryanw3bb / unity-native-toolkit

Native iOS & Android functionality in Unity
MIT License
270 stars 44 forks source link

Android WRITE_EXTERNAL_STORAGE Permission error #22

Open Goncharuk-Nikita opened 5 years ago

Goncharuk-Nikita commented 5 years ago

When i call NativeToolkit.SaveImage I get the exception:

AndroidPlayer(ADB@127.0.0.1:34999) AndroidJavaException: java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=30377, uid=10392 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=30377, uid=10392 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

My manifest permissions: image

External (SD Card checked) image

Unity 2019.1.3f1

Goncharuk-Nikita commented 5 years ago

Minimum API Level set to 24 Traget API Level set to 28

I your documentation write that the target API should be 27, can this cause issue?

alexandre-ludimension commented 5 years ago

Hi, I think I solved this issue by adding the permission request before saving: // Manage Android Permissions

if PLATFORM_ANDROID

    if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
    {
        Permission.RequestUserPermission(Permission.ExternalStorageWrite);
    }

endif

NativeToolkit.SaveImage(m_BGTexture, "Save", "jpg" );

However, the image is not copied into the gallery, (/gallery/ or pictures), but stays in Android/data/myApp/files/Save.jpg

I am using Unity 2019.1 and Android minimum target 27. Even the example project has the same issue.

Moreover, it cannot overwrite this image. Is this normal ? Please help !

Goncharuk-Nikita commented 5 years ago

Thank you for your answer.

Moreover, it cannot overwrite this image. Is this normal ?

For me, i save the each new screenshot with the new file name and do not overwrite there. But I also tested your version: the image is successfully overwritten, but the preview image in the gallery is not updated, then when you click on this image, you see a new one.

private string GetPhotoName()
{
        return $"{_photoSuffix}_{DateTime.Now:dd-MM-yyyy-HH-mm-ss}";
}

However, the image is not copied into the gallery, (/gallery/ or pictures), but stays in Android/data/myApp/files/Save.jpg

This is not important for my task, but I also wanted to know if it is possible?

alexandre-ludimension commented 5 years ago

Thanks for your answer, I also have detected this gallery app update issue.

On iOS this function works perfectly, gallery/update, ect., but not on Android. I guess the update issue has nothing to do with NativeToolkit, however the copy into /Gallery/, IMHO, should be part of the function. Or a copyToDir function.

Thanks for the reply.