file permissions - How to get a DocumentContract using Android Storage Access Framework -


i trying implement saf in app. have managed copy music files external sdcard using following:

         intent intent = new intent(intent.action_open_document_tree);     startactivityforresult(intent, request_code_open_directory); 

which brings file/folder picker

to copy:

   private string copyfile(string inputpath, string inputfile, uri treeuri) {     inputstream in = null;     outputstream out = null;     string error = null;     documentfile pickeddir = documentfile.fromtreeuri(getactivity(), treeuri);     string extension = inputfile.substring(inputfile.lastindexof(".")+1,inputfile.length());      try {         documentfile newfile = pickeddir.createfile("audio/"+extension, inputfile);         out = getactivity().getcontentresolver().openoutputstream(newfile.geturi());         in = new fileinputstream(inputpath + 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 = fnfe1.getmessage();     } catch (exception e) {         error = e.getmessage();     }     return error; } 

but when user selected "move" want delete files in original location(s) may not in documenttree have nothing folder picked. paths taken mediastore_data field. question how can documentcontract files can delete them?

there no way obtain uri permission saf w/o invoking ui.

since use mediastore obtain media file, can use contentresolver.delete() media uri have delete it. assume have write_external_storage permission. don't recommend using file delete underlying file directly mediastore won't have chance clean up.

alternatively, can try https://developer.android.com/reference/android/os/storage/storagevolume.html#createaccessintent(java.lang.string) obtain tree uri permission entire primary storage. works api 24.


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 -