After a lot of findings about how to write(and rename) a file in SD Card (android 5 and above), I think the new SAF provided by android will be required to take permission from user to write SD card file.
I have seen in this File Manger Application ES File Explorer that initially it takes read and write permission the following way as shown in pictures.
After selecting sd card, the write permission is granted.
So in the same way I tried to use SAF, but failed in renaming a file. My code:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rename = (Button) findViewById(R.id.rename); startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), 42); } @Override public void onActivityResult(int requestCode,int resultCode,Intent resultData) { if (resultCode != RESULT_OK) return; 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); } public void renameclick(View v) { File ff = new File("/storage/sdcard1/try1.jpg"); try { ff.createNewFile(); } catch (Exception e) { Log.d("error", "creating"); e.printStackTrace(); } }
Still after running the code I get EAacces permission denied.
Android 4.4 (API level 19) introduces the Storage Access Framework (SAF). The SAF makes it simple for users to browse and open documents, images, and other files across all of their preferred document storage providers.
To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.
The Storage Access Framework should subsequently display the “Save to” storage picker user interface as illustrated in Figure 70-2. From this menu, select the Drive option followed by My Drive and navigate to a suitable location on your Google Drive storage into which to save the file.
Scoped storage limits app access to external storage. In Android 11 or higher, apps targeting API 30 or higher must use scoped storage. Previously in Android 10, apps could opt out of scoped storage.
Letting the user choose the "SD card" or even the "Internal storage" SAF root give your application access to the corresponding storage, but only through the SAF API, not directly via the filesystem. For example you code could be translated into something like :
public void writeFile(DocumentFile pickedDir) { try { DocumentFile file = pickedDir.createFile("image/jpeg", "try2.jpg"); OutputStream out = getContentResolver().openOutputStream(file.getUri()); try { // write the image content } finally { out.close(); } } catch (IOException e) { throw new RuntimeException("Something went wrong : " + e.getMessage(), e); } }
In recent version of Android, accessing data outside your application using java.io.File
is almost completely deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With