简体   繁体   English

在 android 11(sdk 30) 中,如何从下载文件夹上传 pdf 文件? 我收到权限被拒绝的 EACCES 消息

[英]in android 11(sdk 30) ,how to upload pdf file from downloads folder? i am getting permission denied EACCES message

it was working fine in android 10 with requestLegacyExternalStorage="true" tag.它在带有 requestLegacyExternalStorage="true" 标签的 android 10 中运行良好。 the app is like whatsapp, so that user can select file/image/location and send it to another user.该应用程序就像whatsapp,因此用户可以 select 文件/图像/位置并将其发送给另一个用户。 Image and location are working fine, but i am getting error when uploading documents.图像和位置工作正常,但上传文件时出现错误。 I don't think my app qualifies for MANAGE_EXTERNAL_STORAGE permission.我认为我的应用不符合 MANAGE_EXTERNAL_STORAGE 权限。 So if you have any solution, please share.因此,如果您有任何解决方案,请分享。

My file chooser after getting READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE runtime permissions.获得 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 运行时权限后我的文件选择器。

    Intent intent = new Intent();
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(Intent.createChooser(intent, "Select File"), 1);

in OnActivityResult i am getting the document in 'file' that is global variable.在 OnActivityResult 我正在获取全局变量“文件”中的文档。

       Uri uri = data.getData();
       // Log.e("cs","uri =>"+uri);

        file = new File(uri.getPath());

        final String[] split = file.getPath().split(":");//split the path.
        String filePath = split[1];//assign it to a string(your choice).

        //Log.e("cs","filepath=>"+filePath);

        file = new File(filePath);

       // Log.e("cs","file=>"+file.exists());

the problem arise when i am uploading the file当我上传文件时出现问题

    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);

so in retrofit it goes to onFailure and i am getting error:: open failed: EACCES (Permission denied).所以在 retrofit 中它进入 onFailure 并且我收到错误::打开失败:EACCES(权限被拒绝)。

The solution you need to add您需要添加的解决方案

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

in the manifest file and try to get the file access permission in the android 11 phones.在清单文件中,并尝试在 android 11 手机中获取文件访问权限。 Then you will open and read the file from the storage.然后您将打开并从存储中读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM