微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使在授予权限后,也无法在 SDK30 中使用以下代码创建文件

如何解决即使在授予权限后,也无法在 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)

解决方法

getExternalFilesDir() 标志在 Android 11 中已弃用。您需要使用缓存存储或媒体存储。

,

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

 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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。