0

I have received a content uri to an image received as a result from an activity*

I am simply trying to copy that file to another file, but I keep getting permission denied when I try to open the destination file. Anyone know what I am doing wrong?

    Uri u = Uri.parse( data.getDataString());
    ContentResolver contentResolver = getContentResolver();

    try {
        InputStream is = contentResolver.openInputStream(u);

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // media is not available to use. Abort.
            Toast.makeText(this, "Storage Unavailable.", Toast.LENGTH_LONG).show();
            return;
        }

        String dir = Environment.getExternalStorageDirectory().toString();
        File f = new File(dir, "test.jpg");
        f.createNewFile();
        // exception thrown
        // java.io.FileNotFoundException: /storage/emulated/0/test.jpg (Permission denied)
        FileOutputStream fose = new FileOutputStream(f); 

        byte[] b = new byte[1024];

        while (is.read(b) != -1) {
            fileOutputStream.write(b);
            fose.write(b);
        }
        fileOutputStream.close();
        is.close();
    } catch (IOException e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    ImageView iv = (ImageView) findViewById(R.id.theimage);
    iv.setImageURI(u);

The permission for external write is declared in the manifest, and runtime permission has been granted.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

*Here's the intent used

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
   startActivityForResult(Intent.createChooser(intent, "Select a File"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}

Edit: full stack trace:

6 15:23:27.068 6181-6181/test.example.x W/System.err: java.io.IOException: Permission denied
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.File.createNewFile(File.java:948)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at test.example.x.ui.MainActivity.onActivityResult(MainActivity.java:173)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6915)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.-wrap20(ActivityThread.java)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Permission is granted: https://i.stack.imgur.com/RWqxV.png

Canoe
  • 589
  • 5
  • 8
  • What API version? See http://stackoverflow.com/questions/33162152/storage-permission-error-in-marshmallow if on any version with runtime permission checking – Gabe Sechan Apr 06 '17 at 20:46
  • You need to ask to user or grant permission from settings app. please refer https://developer.android.com/training/permissions/requesting.html – Murli Prajapati Apr 06 '17 at 20:46
  • @Gabe Runtime permission has already been granted. SDK ver 25 – Canoe Apr 06 '17 at 20:47
  • @MuraliPrajapati Runtime permission has been granted, and I am still getting this error. – Canoe Apr 06 '17 at 20:48
  • try running lower android version device to check whether the problem is related to permission or anything else. – Murli Prajapati Apr 06 '17 at 21:10
  • or use getExternalStorageDirectory().getAbsolutePath() instead of toString() method. – Murli Prajapati Apr 06 '17 at 21:11
  • Please edit your question and post the entire Java stack trace associated with your crash. – CommonsWare Apr 06 '17 at 21:30
  • @MuraliPrajapati using getAbsloutePath() instead did not help. However, everything did work when I ran it on KitKat (19). Hmmmm... – Canoe Apr 06 '17 at 21:43
  • @CommonsWare Edits added. – Canoe Apr 06 '17 at 21:44
  • OK, that's fairly bizarre. If this is an emulator, try creating a fresh AVD with a fresh block of external storage. – CommonsWare Apr 06 '17 at 21:50
  • @CommonsWare Ok, Just did a full download -> fresh emulator of API 24 with Google APIs and I am getting the exact same thing – Canoe Apr 06 '17 at 22:02
  • I do not know why you are getting these results. Try dropping your `targetSdkVersion` to 22, as that will eliminate the need for the runtime permission. If that clears up the problem, then somehow runtime permissions are the issue, despite your screenshot. – CommonsWare Apr 06 '17 at 22:08

1 Answers1

1

I was experiencing the same problem and it can be easily removed by following either of these steps: 1. Install your app by using -g if installing on Android N and O versions. 2. Grant permission manually Settings->apps->"app_name"->Permissions->Enable Switch

For Both steps 1 and 2, define uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" and uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" attribute in AndroidManifest.xml

Vsw10
  • 143
  • 3
  • 10