I can't copy or move the file from U disk when I run my code on Android P and throw the exception said 'permission denied'. the same code runs on 8.0 or lower works good. I use the FileInputStream and FileOutputStream to copy the file in U disk.
I've declared permission about android.permission.READ_EXTERNAL_STORAGE
and android.permission.WRITE_EXTERNAL_STORAGE
. But it still doesn't work.
private int copyFileUsingFileStreams(File source, File dest)
throws IOException {
int resultCode = 0;
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[2048];
int bytesRead;
while ((bytesRead = input.read(buf)) != -1) {
if (isInterrupted()) {
resultCode = ZFileManager.OperateResult.USER_CANCLE;
input.close();
output.close();
break;
}
output.write(buf, 0, bytesRead);
}
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
}
return resultCode;
}