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

Permission denied while it Allowed! access to external storage problem #226

Closed
osamz opened this issue Feb 2, 2020 · 8 comments
Closed

Comments

@osamz
Copy link

osamz commented Feb 2, 2020

I tried the sample app on Android 10 (AVD virtual device), and it failed to saving photo on device, "permission denied", while I Allowed it.

I search and found that this deprecated function is the problem.

File file = new File(Environment.getExternalStorageDirectory()

I replace it with:
File file = new File(this.getExternalFilesDir(DIRECTORY_PICTURES) + File.separator + "" + System.currentTimeMillis() + ".png");
and it works fine, but it save photo in app private folder.

  • I read that android 10 will not give direct access to external storage.

there are some workaround solutions, but I didn't use it.

@burhanrashid52
Copy link
Owner

Yes. I am aware of it. If would be really great if you can help to raise a PR for Android 10 storage support

@swentel
Copy link

swentel commented Feb 7, 2020

FWIW, I was able to save it to the DCIM folder in a custom app I'm writing.

    String FOLDER_NAME = "Subfolder";
    File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), FOLDER_NAME);
    if (!directory.exists()) {
        exists = directory.mkdirs();
    }

    File file = null;
    if (exists) {
        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                + File.separator + ""
                + FOLDER_NAME
                + File.separator + ""
                + System.currentTimeMillis() + ".png");
      }

I also added a media scan in the onSuccess method so the new file shows up immediately in the gallery apps

    MediaScannerConnection.scanFile(EditImageActivity.this, new String[] { finalFile.getAbsolutePath() }, null, null);

I then pass on the imageUri back to the calling activity where in onActivityResult I ask for permissions so the app can read/write the newly written file:

    final int takeFlags = data.getFlags()
        & (Intent.FLAG_GRANT_READ_URI_PERMISSION
        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    getContentResolver().takePersistableUriPermission(uri, takeFlags);

Hope that might help a bit.

@osamz
Copy link
Author

osamz commented Feb 8, 2020

thanks, @swentel, @burhanrashid52

@breel93
Copy link

breel93 commented Mar 27, 2020

I just Opened PR about this Issue,
My fix:
app:manifest.xml

<application
        android:name="com.burhanrashid52.photoeditor.PhotoApp"
        ........
        android:requestLegacyExternalStorage="true">

EditImageActivity
Replace

 File file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + ""
                    + System.currentTimeMillis() + ".png");

with

final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                System.currentTimeMillis() + ".png");

then add this method

//added the photo file to gallery
    private static void galleryAddPic(Context context, String imagePath) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(imagePath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        context.sendBroadcast(mediaScanIntent);
    }

call the method after in saved onSuccess

    @Override
     public void onSuccess(@NonNull String imagePath) {
        ...........
        mPhotoEditorView.getSource().setImageURI(mSaveImageUri);
        galleryAddPic(EditImageActivity.this, file.getAbsolutePath());  <-- here
       }

I hope this helps someone

@nazmussakibsaad102
Copy link

@breel93 Thanks bro. Helped a lot :D

@Aldinsan1453
Copy link

Hi.. i'm still experiencing permission denied... while permission is already given in manifest... and also tried solution above but.. still permission is denied

@bjdev222
Copy link

check whet

Hi.. i'm still experiencing permission denied... while permission is already given in manifest... and also tried solution above but.. still permission is denied

check whether u give permission to access storage from mobile setting for this app..

@burhanrashid52
Copy link
Owner

Fixed in #326

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants