I am making an android app which lets the user transfer some files from internal storage to external storage(SD card) using SAF.
This is how I call an intent to let the user choose the SD card directory.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, 42);
And this is my onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent data) {
treeUri = data.getData();
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getActivity().getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
}
I am taking permission for whole SD card so that i can move or delete any file in sd card any time .The problem is that even after taking permission i am not able to move or delete any file in SD card.
How should i move a file from internal Storage to external(SD card) for android 5.0 and above.
EDIT:This is the code i am using to move files.
public static void MoveFiles(File src, File dst) throws IOException
{
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try
{
inChannel.transferTo(0, inChannel.size(), outChannel);
}
finally
{
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
src.delete();
}
}
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
. Statement makes no sense. Better remove it. – greenapps Dec 19 '16 at 17:37