Android M write to SD Card - Permission Denied -


i'm trying copy file within application sd card, error eacces (permission denied). os android m , have allowed runtime storage permissions (checked in app info). have set uses-permission in androidmanifest.xml

<application>...</application> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" /> 

doesn't work if copy sd card

source: data/user/0/com.example.myapp/cache/somefile.txt destination: /storage/1032-2568/somefolder/ error: java.io.filenotfoundexception: /storage/1032-2568/somefolder/somefile.txt: open failed: eacces (permission denied) 

works if copy internal storage

source: data/user/0/com.example.myapp/cache/somefile.txt destination: /storage/emulated/0/somefolder/ 

code copy file source destination

/*  * below parameters have tried  *  * inputpath - data/user/0/com.example.myapp/cache or data/user/0/com.example.myapp/cache/  * inputfile - /somefile.txt or somefile.txt  * outputpath - /storage/1032-2568/somefolder/ or /storage/1032-2568/somefolder  */ public static void copyfile(string inputpath, string inputfile, string outputpath) {      inputstream in = null;     outputstream out = null;     try {          //create output directory if doesn't exist         file dir = new file (outputpath);         if (!dir.exists()) {             dir.mkdirs();         }          in = new fileinputstream(inputpath + inputfile);         out = new fileoutputstream(outputpath + inputfile);          byte[] buffer = new byte[1024];         int read;         while ((read = in.read(buffer)) != -1) {             out.write(buffer, 0, read);         }         in.close();           // write output file (you have copied file)         out.flush();         out.close();     }     catch (filenotfoundexception fnfe1) {         /* error here */         log.e("tag", fnfe1.getmessage());     }     catch (exception e) {         log.e("tag", e.getmessage());     } } 

es file explorer

i saw es file explorer cannot write on sd card on redmi devices. here's video solution. following steps worked es explorer on device. can done programmatically?

as suggested @commonsware here have use new storage access framework provided android , have take permission user write sd card file said written in file manager application es file explorer.

here code letting user choose "sd card" :

startactivityforresult(new intent(intent.action_open_document_tree), requestcode); 

which :

enter image description here

and document path in pickeddirand pass further in copyfile block , use path writing file :

public void onactivityresult(int requestcode, int resultcode, intent resultdata) {     if (resultcode != result_ok)         return;     else {         uri treeuri = resultdata.getdata();         documentfile pickeddir = documentfile.fromtreeuri(this, treeuri);         granturipermission(getpackagename(), treeuri, intent.flag_grant_read_uri_permission | intent.flag_grant_write_uri_permission);         getcontentresolver().takepersistableuripermission(treeuri, intent.flag_grant_read_uri_permission | intent.flag_grant_write_uri_permission);         copyfile(sdcard.tostring(), "/file.txt", path + "/new", pickeddir);     } }   public void copyfile(string inputpath, string inputfile, string outputpath, documentfile pickeddir) {      inputstream in = null;     outputstream out = null;     try {          //create output directory if doesn't exist         file dir = new file(outputpath);         if (!dir.exists()) {             dir.mkdirs();         }          in = new fileinputstream(inputpath + inputfile);         //out = new fileoutputstream(outputpath + inputfile);          documentfile file = pickeddir.createfile("//mime type", outputpath);         out = getcontentresolver().openoutputstream(file.geturi());          byte[] buffer = new byte[1024];         int read;         while ((read = in.read(buffer)) != -1) {             out.write(buffer, 0, read);         }         in.close();           // write output file (you have copied file)         out.flush();         out.close();     } catch (filenotfoundexception fnfe1) {     /* error here */         log.e("tag", fnfe1.getmessage());     } catch (exception e) {         log.e("tag", e.getmessage());     } } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -