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
 
    