访问文件创建抛出错误打开失败:android 11中的EACCES(权限被拒绝)

访问文件创建抛出错误打开失败:android 11中的EACCES(权限被拒绝),android,Android,我使用创建了一个文件 File File=new文件(Environment.getExternalStorageDirectory().getPath(),“MyFolder/Images/newImg.jpg”) 当我试图使用 FileOutputStream out=新的FileOutputStream(file.getAbsolutePath()); 它把这个错误抛给了我 java.io.FileNotFoundException:/storage/simulated/0/MyFolde

我使用创建了一个文件 File File=new文件(Environment.getExternalStorageDirectory().getPath(),“MyFolder/Images/newImg.jpg”)

当我试图使用 FileOutputStream out=新的FileOutputStream(file.getAbsolutePath()); 它把这个错误抛给了我 java.io.FileNotFoundException:/storage/simulated/0/MyFolder/Images/ig_1619857233792.jpg:打开失败:EACCES(权限被拒绝)

之后,我还尝试使用MediaStore制作文件 然后它把这个错误抛给了我 java.io.FileNotFoundException:content:/media/external/images/media/109166:打开失败:enoint(没有这样的文件或目录)

该应用程序还获得了android 11中的“所有文件访问”权限,以及android 11下的“读取外部存储”和“写入外部存储”权限

public void onDocumentSelectionResult(Intent data) {
    try {
        Uri uri = null;
        fileList = new ArrayList<>();
        fileType = new ArrayList<>();
        fileName = new ArrayList<>();
        String displayName = null;

        if (data != null) {
            String[] filePathColumn = {MediaStore.Files.FileColumns.DATA};
            String filePath;
            try {
                if (data.getData() != null) {
                    uri = data.getData();

                    String uriString = uri.toString();
                    File myFile = new File(uriString);
                    if (uriString.startsWith("content://")) {
                            Cursor cursor = null;
                            try {
                                cursor = getContentResolver().query(uri, null, null, null, null);
                                if (cursor != null && cursor.moveToFirst()) {
                                    displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                                }
                            } finally {
                                cursor.close();
                            }
                        } else if (uriString.startsWith("file://")) {
                            displayName = myFile.getName();
                        }
                    fileName.add(displayName);
                    filePath = uri.getPath();
                    File sourceFile = new File(filePath);
                    long file_size = Long.parseLong(String.valueOf(sourceFile.length() / 1024));
                    ContentResolver cr = getBaseContext().getContentResolver();
                    String mime = cr.getType(uri);
                    if (mime == null || "".equals(mime)) {
                        mime = filePath.substring(filePath.lastIndexOf("."));
                    }     
                } else {
                    if (data.getClipData() != null) {
                        ClipData mClipData = data.getClipData();
                        for (int i = 0; i < mClipData.getItemCount(); i++) {
                            ClipData.Item item = mClipData.getItemAt(i);
                            uri = item.getUri();

                            String uriString = uri.toString();
                            File myFile = new File(uriString);

                            if (uriString.startsWith("content://")) {
                                Cursor cursor = null;
                                try {
                                    cursor = getContentResolver().query(uri, null, null, null, null);
                                    if (cursor != null && cursor.moveToFirst()) {
                                        displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                                    }
                                } finally {
                                    cursor.close();
                                }
                            } else if (uriString.startsWith("file://")) {
                                displayName = myFile.getName();
                            }
                            fileName.add(displayName);
                            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                                filePath = getPath(ActivityUploadPost.this, uri);
                            } else {
                                filePath = uri.getPath();
                            }
                         //   filePath = getPath(ActivityUploadPost.this, uri);
                            File sourceFile = new File(filePath);
                            long file_size = Long.parseLong(String.valueOf(sourceFile.length() / 1024));
                            ContentResolver cr = getBaseContext().getContentResolver();
                            String mime = cr.getType(uri);
                        }
                    }
                }
    } catch (Exception e) {
        e.printStackTrace();
    }
}



File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyFolder/Images");
if(!file.exists()) {
  file.mkdirs();
}
String uriString = (file.getAbsolutePath() + File.separator + "ig_" + System.currentTimeMillis() + ".jpg");
FileOutputStream out = null;
try {
  out = new FileOutputStream(uriString);
  scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (Exception ex) {
  ex.printStackTrace();
}
DocumentSelectionResult(意图数据)上的公共作废{
试一试{
Uri=null;
fileList=newarraylist();
fileType=newarraylist();
fileName=newarraylist();
字符串displayName=null;
如果(数据!=null){
字符串[]filePathColumn={MediaStore.Files.FileColumns.DATA};
字符串文件路径;
试一试{
if(data.getData()!=null){
uri=data.getData();
字符串uriString=uri.toString();
File myFile=新文件(uriString);
if(uriString.startsWith(“content://”){
游标=空;
试一试{
cursor=getContentResolver().query(uri,null,null,null,null);
if(cursor!=null&&cursor.moveToFirst()){
displayName=cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
}最后{
cursor.close();
}
}else if(uriString.startsWith(“文件:/”){
displayName=myFile.getName();
}
添加(显示名称);
filePath=uri.getPath();
文件源文件=新文件(文件路径);
long file_size=long.parseLong(String.valueOf(sourceFile.length()/1024));
ContentResolver cr=getBaseContext().getContentResolver();
字符串mime=cr.getType(uri);
如果(mime==null | |“”.equals(mime)){
mime=filePath.substring(filePath.lastIndexOf(“.”);
}     
}否则{
if(data.getClipData()!=null){
ClipData mClipData=data.getClipData();
对于(int i=0;i
您应该首先检查目录是否存在。如果不存在,请在尝试将文件放入目录之前创建它们。您好@blackapps,我检查了目录和文件是否同时存在。我发现目录存在且文件不存在。我正在使用“ACCESS\u MEDIA\u LOCATION”,文件文件=新文件(Environment.getExternalStorageDirectory().getPath(),“MyFolder/Images/newImg.jpg”)在我的案例中创建了一个目录,但没有文件。请在您的帖子中发布完整的代码。我不知道您在做什么。永远不要在注释中发布代码。并在代码块中发布代码。@blackapps,我在获取exc的地方也发布了我的代码