Android 6.0+ java.io.FileNotFoundException: (Permission denied)

11,478

Luckily, I solved my problem finally. Here was the reason: The storage permission conflicts with the dependency below:

compile 'com.github.vungle:vungle-android-sdk:5.3.0'

When I added the dependency, system cannot recognized the storage permission which I have apply for(while I added twice in AndroidManifest.xml, it worked), but when I removed it, everything was ok ! This dependency was used to add ad into app, but now I have to abandon it.

Share:
11,478
Jason Wu
Author by

Jason Wu

Updated on June 04, 2022

Comments

  • Jason Wu
    Jason Wu almost 2 years

    Recently, I have met a really strange problem about permission issue, it confused me a few days, I hope someone will help me figure out what is going on. Thanks!

    Here is the problem: For some reason, I need to apply the storage permission for save some photos, and I had added the permission to the AndroidManifestival.xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    Off course, I knew I have to apply for storage permission in code after Android 6.0, and here is my code for apply for permission:

    public static boolean isGrantExternalRW(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (context.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
    
            ((Activity)context).requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, Constants.PERMISSION_REQUEST_CODE);
    
            return false;
        }
    
        return true;
    }
    

    I had use this method before I saved photo, and the permission dialog had shown up and I had checked it. And I had checked the app permission in the app setting, it's ok.

    And here is the method I used to save the bitmap to the file:

    public static String savePhotoToSDCard(Bitmap bitmap, String photoName) {
        String absolutePath = "";
    
        String state = Environment.getExternalStorageState();
        Log.e("file", "---------------------" + state);
        File newsDir =  new File(Environment.getExternalStorageDirectory().toString()
                + File.separator + "photo");
    
    
        if (!newsDir.exists()) {
            newsDir.mkdirs();
        }
    
        File picFile = new File(newsDir, photoName + ".png");
        absolutePath = picFile.getAbsolutePath();
    
        if (picFile.exists()) {
            picFile.delete();
        }
    
        if (photoName.equals("avatar")) {          
            int ratio = 2;        
            Bitmap result = Bitmap.createBitmap(bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);
            Rect rect = new Rect(0, 0, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio);
            canvas.drawBitmap(bitmap, null, rect, null);
        }
    
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(picFile);
            if (bitmap != null) {
    
                if (bitmap.compress(Bitmap.CompressFormat.PNG, 30, fileOutputStream)) {
                    fileOutputStream.flush();
                    Log.d("Photo", "---------------------------fileOutputStream.flush");
                    fileOutputStream.close();
                }
            }
        } catch (FileNotFoundException e) {
            Log.e("File", "----------------------------" + e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return absolutePath;
    }
    

    When I ran the app, it went wrong, here is the log:

    java.io.FileNotFoundException: /storage/emulated/photo/avatar.png(permission denied)

    But the most strange thing was that when I add the storage permission twice to AndroidManifestival.xml, it worked!

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    So, what's the problem? Please help.

    • Abdul Waheed
      Abdul Waheed over 6 years
      try again with removing first set of permissions and see the same problem occurs again or not
    • greenapps
      greenapps over 6 years
      FileNotFoundException: /storage/emulated/0/android/mktaid/avatar.png ? That is not a possible path if you used the code you posted. As it does not match File newsDir = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "photo");.
    • greenapps
      greenapps over 6 years
      Moreover a path like /storage/emulated/0/android/..... cannot exist as there is already a /storage/emulated/0/Android directory.
    • Jason Wu
      Jason Wu over 6 years
      I had removed first set of permissions but the problem was occured again
    • Jason Wu
      Jason Wu over 6 years
      I put the wrong log before, now I corrected
    • Nawrez
      Nawrez over 6 years
      can you add your code
    • greenapps
      greenapps over 6 years
      newsDir.mkdirs(); . Change to if(! newsDir.mkdirs()){Toast(...sorry cannot create directory...); return;}
  • Grimthorr
    Grimthorr over 6 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • Nawrez
    Nawrez over 6 years
    @Grimthorr can you check the last edit ? and give me a feedback
  • greenapps
    greenapps over 6 years
    There is no need to implement onRequestPermissionResult(). The result is granted or not. Independend from if you add that function. And OP said that in Settings the toggle was ON. So i do not understand that you even started to talk about permissions.
  • Nawrez
    Nawrez over 6 years
    @Grimthorr : in my turn, I would like to thank you for your support !
  • Grimthorr
    Grimthorr over 6 years
    @HadjKhelilNawrez The edited answer is much better. However, I think it was also flagged because the content doesn't quite match the question. This is why link-only answers are normally better suited as a comment until you're sure the question can be answered from information in the link.
  • Jason Wu
    Jason Wu over 6 years
    @HadjKhelilNawrez I had try onRequestPermissionResult(), but it didn't worked.
  • Ashish Nair
    Ashish Nair almost 6 years
    Yes i also had vungle library integrated, its working.Strange Issue