首页 > 解决方案 > 即使授予权限后,也无法在 SDK30 中使用以下代码创建文件

问题描述

FileOutputStream outputStream = null;
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        //String string = Environment.getExternalStorageDirectory().toString();
        File dir = new File(file + "/MyPics");
        dir.mkdirs();
        
        String filename = String.format("%d.jpg",System.currentTimeMillis());
        Log.e(TAG,"  saveToGallery filename" + filename);

        File outFile = new File(dir,filename);
        Log.e(TAG,"  saveToGallery outFile " + outFile.toString() + " Output file exists : " + outFile.exists());
        if (outFile.exists ()) {
            outFile.delete();
            Log.e(TAG,"  saveToGallery outFile.delete() " );
        }
        if(outFile.createNewFile()) {
            Log.e(TAG, "  saveToGallery outFile.createNewFile() " + outFile.toString() + " Output file exists : " + outFile.exists());
            try {
                outputStream = new FileOutputStream(outFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.flush();
                outputStream.close();

                Log.e(TAG, "  saveToGallery outputStream " + outputStream.toString());

            } catch (Exception e) {
                Log.e(TAG, "  saveToGallery Creating outputStream failed ");
                e.printStackTrace();
            }

错误日志:

2020-11-04 05:07:38.479 24501-24501/com.mtech.mtechproject E/ checkPermission Function: Granted permission for writing: 101
2020-11-04 05:07:38.479 24501-24501/com.mtech.mtechproject E/BaseActivity:   saveToGallery ++
2020-11-04 05:07:38.486 24501-24501/com.mtech.mtechproject E/BaseActivity:   saveToGallery filename1604446658485.jpg
2020-11-04 05:07:38.488 24501-24501/com.mtech.mtechproject E/BaseActivity:   saveToGallery outFile /storage/emulated/0/Pictures/MyPics/1604446658485.jpg Output file exists : false
2020-11-04 05:07:38.490 24501-24501/com.mtech.mtechproject W/System.err: java.io.IOException: No such file or directory
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err:     at java.io.File.createNewFile(File.java:1008)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err:     at com.mtech.mtechproject.BaseActivity.saveToGallery(BaseActivity.java:306)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err:     at com.mtech.mtechproject.BaseActivity.checkPermission(BaseActivity.java:266)

标签: androidandroid-11

解决方案


使用以下代码检查您的版本是否 >= Android 11,如果使用 getExternalFilesDir() 标志,getExternalStorageDirectory() 将不起作用。在下面的函数中,布尔值将返回文件是否存在于外部存储中。

 public boolean checkImageInLocalStorage(String fileName, String folderName, String folderType){
    boolean isPresent =false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
        File rootPath= context.getExternalFilesDir("AppFolderName").getAbsoluteFile();
        String rootFolder =Constants.DOWNLOAD_DIRECTORY_NAME;

        File localFile = new File(rootPath+"/"+rootFolder+"/"+folderType+"/"+folderName+"/"+fileName);
        if (localFile.exists() && !localFile.isDirectory()){
            isPresent = true;
        }
    }else {

        File rootPath = Environment.getExternalStorageDirectory();
        String root = rootPath.getAbsolutePath().toString();
        String rootFolder = Constants.DOWNLOAD_DIRECTORY_NAME;

        //  File rootPath=context.getFilesDir();
        File localFile = new File(rootPath.getAbsoluteFile().getAbsolutePath());
        localFile = new File(root + "/" + rootFolder + "/" + folderType + "/" + folderName + "/" + fileName);

        if (localFile.exists() && !localFile.isDirectory()) {
            // do something
            isPresent = true;
        }
    }
    return isPresent;
}

推荐阅读