gpt4 book ai didi

android - 无法在 FileSelector android 中选择最近的文档

转载 作者:行者123 更新时间:2023-12-05 07:41:43 30 4
gpt4 key购买 nike

我允许用户从本地存储中选择特定类型的文件 (pdf,docx,xlsx) 以将其上传到 php 服务器

1 ) 当用户通过FileExplorer , ESFileExplorer 等已安装的应用程序选择文件时,他可以将文件成功上传到服务器

所选文件的URI:content://media/external/file/104310

文件路径选择文件:/storage/emulated/0/Download/Bipasha-Basu-Cover-of-Noblesse-India_August_2014_08A.pdf

2 ) 当用户通过FileSelector(Android 默认)最近选择文件时,他无法上传文件到服务器.

所选文件的URI:content://com.android.providers.downloads.documents/document/28433

文件路径选择文件 :/data/user/0/com.android.providers.downloads/cache/enquiry_chat_1044781500443064.pdf

错误消息: java.io.FileNotFoundException:/data/user/0/com.android.providers.downloads/cache/enquiry_chat_1044781500443064.pdf(权限被拒绝)

我的代码:

public static void openDocuments(Activity activity) {
Intent intent = new Intent();
intent.setType("*/*");
String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword","application/pdf","application/docx","application/xlsx","application/pptx","application/pptx","application/txt"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
intent.setAction(Intent.ACTION_GET_CONTENT);
activity.startActivityForResult(Intent.createChooser(intent, "Choose a file to send..."), MediaConstants.DOCUMENT_REQUEST);
}

AndroidManifest.xml

    <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:readPermission="com.android.providers.READ_DATABASE"
android:writePermission="com.android.providers.WRITE_DATABASE"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>

最佳答案

Uri contentUri = null;

if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
else if ("document".equals(type)) { //add this in your getPath method
contentUri = MediaStore.Files.getContentUri("external");
}

关于android - 无法在 FileSelector android 中选择最近的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45206524/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com