0

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;
}
Molly
  • 1,887
  • 3
  • 17
  • 34
  • you need to explicitly request for permission. declaring uses-permission in android manifest is not enough. refer to https://developer.android.com/training/permissions/requesting – Angel Koh Aug 22 '19 at 04:00
  • there must be some differences between 8.0 and 9.0, the same code runs on 8.0 is fine but 9.0. – Darren.Chen Aug 22 '19 at 06:12

0 Answers0