它在 android 10 中运行良好,带有 requestLegacyExternalStorage="true" 标签。该应用程序就像whatsapp,以便用户可以选择文件/图像/位置并将其发送给另一个用户。图像和位置工作正常,但我在上传文档时遇到错误。我认为我的应用不符合 MANAGE_EXTERNAL_STORAGE 权限。所以如果您有任何解决方案,请分享。
获得 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);
Run Code Online (Sandbox Code Playgroud)
在 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());
Run Code Online (Sandbox Code Playgroud)
当我上传文件时出现问题
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
Run Code Online (Sandbox Code Playgroud)
所以在改造中它会转到 onFailure 并且我收到错误 :: open failed: …