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

如何使用 okhttp 和内容 URI 在 Android 11 中将文件上传到服务器? (PDF, docx..)

如何解决如何使用 okhttp 和内容 URI 在 Android 11 中将文件上传到服务器? (PDF, docx..)

我在 android 中使用 okhttp 库将文件上传到服务器。 我以前从存储访问框架返回的URI中获取文件的真实路径然后上传

在 android 11 中,我能够获取媒体文件的真实路径,但对于非媒体文件,例如文档“content://com.android.providers.media.documents/document/document” >XX”我得到一个空路径。 “content://com.android.providers.media.documents/document/imageXX”可以通过返回 /storage/emulated/0/Download/xxx.jpeg

正常工作

有人可以向我提供在 android 11 中获取文档真实路径的方法吗?或者如果有另一种上传文件的方式,而无需获取真实路径。

谢谢,

        String path = FileUtils.getPath(this,uri);//Get the absolute path of the file through uri
  
        File file = new File(path);//Create a specific file class
        String fileName = file.getName();//To get the file name is mainly the suffix of the file name

        String type = getContentResolver().getType(uri);
        RequestBody requestBody = RequestBody.create(file,MediaType.parse(type));
        ...

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