I want to copy a file from external storage to my folder that exists in external storage. I choose the file using
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(chooseFile, 1);
and try to copy this file in activityResult using:
Uri selected = data.getData();
String[] filePathColumn = { MediaStore.Files.FileColumns.DATA};
Cursor cursor = getContentResolver().query(selected,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String FilePath = cursor.getString(columnIndex);
cursor.close();
FileInputStream fis = new FileInputStream(FilePath);
String outFileName = Environment.getExternalStorageDirectory() + "/MyPath/" + "MyFileName";
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
fis.close();
but something is wrong cause I get null in FilePath.How can I do this?