Writing to external SD card in Android 5.0 and above
Android: EACCES
(Permission denied)
Writing files in Android system is easy with respect to
internal storage, but when it comes to writing in external removable storage it
may be a problem after android 4.4 release.
Apart from android 6 permission we need to gain permission
to write in external removable storage, as it can throw a EACCES (Permission
denied) Error even if android.permission.WRITE_EXTERNAL_STORAGE is granted.
So the following code will ensure that you can write in
External media with the necessary permission apart from android.permission.WRITE_EXTERNAL_STORAGE.
Call the following function to open the system dialog to
gain access with external removable storage.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
also the Activity Result Listener as follows:
startActivityForResult(intent, 42);
@Override
public void
onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode
== RESULT_OK && requestCode==42) {
treeUri =
data.getData();
//Todo the following step to make Persist access permissions
final int takeFlags = data.getFlags()& (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getActivity().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
//Todo the following step to make Persist access permissions
final int takeFlags = data.getFlags()& (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getActivity().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
}
}
DocumentFile pickedDir =
DocumentFile.fromTreeUri(getContext(),treeUri);
DocumentFile createFolder=pickedDir.createDirectory("Hello");
DocumentFile newFile= createFolder.createFile("*","my.txt");
try {
OutputStream out =
getContext().getContentResolver().openOutputStrea(newFile.getUri());
out.write("Finally
Writing”.getBytes());
out.close();
}catch
(Exception e){
Log.wtf("Error",e.toString());
}
import java.io.OutputStream;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.provider.DocumentFile;
public class Main extends AppCompatActivity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
}
@Override
public void
onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode
== RESULT_OK && requestCode==42) {
Uri treeUri
= data.getData();
//Todo the following step to make Persist access permissions
final int takeFlags = data.getFlags()& (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getActivity().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
getActivity().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
DocumentFile pickedDir =
DocumentFile.fromTreeUri(getContext(),treeUri);
DocumentFile createFolder=pickedDir.createDirectory("Hello");
DocumentFile newFile=
createFolder.createFile("*","my.txt");
try {
OutputStream out =
getContext().getContentResolver().openOutputStream(newFile.getUri());
out.write("Finally Writing”.getBytes());
out.close();
}catch
(Exception e){
Log.wtf("Error",e.toString());
}
}
}
}
To check weather Permission is granted on a Folder, use following
final List<UriPermission> list = rootView.getContext().getContentResolver().getPersistedUriPermissions();
Remain blessed brother. Your solution worked for me. THanks.
ReplyDeleteI'm glad this article helped you. 😇
Delete