Sagar Maiyad
Sagar Maiyad

Reputation: 12733

java.io.FileNotFoundException: (Permission denied) in oreo android

The following code is for writing contact details in file which is created in sdcard(External storage):

try {
                while (phones.isAfterLast() == false) {

                    String lookupKey = phones.getString(phones
                            .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Uri uri = Uri.withAppendedPath(
                            ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);

                    AssetFileDescriptor fd;


                    fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
                    FileInputStream fis = fd.createInputStream();
                    byte[] buf = readBytes(fis);
                    fis.read(buf);
                    String VCard = new String(buf);
                    FileOutputStream mFileOutputStream = new FileOutputStream(
                            cpath, true);
                    mFileOutputStream.write(VCard.getBytes());
                    phones.moveToNext();
                }
            } catch (Exception e1) {
                Applog.e(TAG, e1);
                isOk = false;
            }

Where, cpath is my file's path which is in sd card.

Here, i am getting error on line:

FileOutputStream mFileOutputStream = new FileOutputStream(
                            cpath, true);

I am getting below log:-

java.io.FileNotFoundException: /storage/9016-4EF8/AllBackupDemo/cont20181031050512.vcf (Permission denied)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:287)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:142)

Document tree selection code:

private void triggerStorageAccessFramework() {
        startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), REQUEST_SAF_PERMISSION);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        Log.e(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data + ")");
        if (resultCode == RESULT_CANCELED) return;

        switch (requestCode) {
            case REQUEST_SAF_PERMISSION:{
                Uri treeUri = data.getData();
                if(treeUri != null){
                    session.setPrefURI(treeUri.toString());
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
                            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
                toast(getString(R.string.permission_granted_saf));
                break;
            }
        }
    }

NOTE:

  1. I am getting this error only in android oreo version. its working fine in nougat.
  2. I already given runtime permission and also mentioned in manifest file.
  3. I alredy implemented SAF for document file permission.

Upvotes: 0

Views: 3941

Answers (1)

Solution:

I analyzed and compared my coding with that of yours and found out that all the code you have used is same.

The line in which you are getting the error must be changed. This error is due to usage of FileOutputStream which indicates that you are accessing from the file system and not from the DocumentTree.

Hence, instead of:

FileOutputStream mFileOutputStream = new FileOutputStream(
                        cpath, true);

Try something like this:

try {
    OutputStream out = getContentResolver().openOutputStream(cpath.getUri());
    try {

        // Do whatever you wish to do.

    } finally {
        out.close();
    }

} catch (IOException e) {
    throw new RuntimeException("Something went wrong : " + e.getMessage(), e);
}

I am not sure about the actual flow of your program, you might want to go through yourself. but, this will definitely give you an idea to go ahead.

Hope it helps.

Upvotes: 1

Related Questions