再会,
我正在制作一个应用程序,我必须从数据库中加载一些带有数据和图像或文档的卡片视图,当按下此元素以获取下载文件的选项并完成下载后,提供从应用。在打开文件(图像、word 文档、excel 等)之前,所有这些都运行良好,此时它不会通过 android studio 控制台或应用程序本身向我显示任何错误,仅在打开文件时它显示为已损坏(当它是图像时,尽管如果我从文件资源管理器中打开它,它会毫无问题地打开),而当它是文档时,它会像文件为空或像新文件一样打开。
所以我通过下载管理器获取数据:
private void executeDownload() {
ProgressDialog progressDialog = new ProgressDialog(AssignedTasksDetailActivity.this);
progressDialog.setMessage("Descargando Archivo...");
progressDialog.setCancelable(false);
progressDialog.show();
// registrer receiver in order to verify when download is complete
registerReceiver(new DonwloadCompleteReceiver(), new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url_app));
request.setDescription("Descargando Archivo " + filename);
request.setTitle("Descargado "+ filename);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
progressDialog.dismiss();
}
然后我在下载完成时显示一个警报对话框,以提供打开文件的选项(这是我认为它失败的地方):
public class DonwloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AssignedTasksDetailActivity.this);
alertDialogBuilder.setTitle("Importante");
alertDialogBuilder.setMessage("¿Desea abrir este archivo?");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
alertDialogBuilder.setPositiveButton("Abrir",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
cancel();
Coop app = ((Coop)getApplicationContext());
//File file = new File(filename); // -> filename = maven.pdf
File file = new File(Environment.DIRECTORY_DOWNLOADS, filename);
Uri path = FileProvider.getUriForFile(AssignedTasksDetailActivity.this, "com.example.coop.fileprovider", file);
String type = url_app.substring(url_app.lastIndexOf('.'), url_app.length());
Intent intent = new Intent(Intent.ACTION_VIEW);
if(type==".jpg" || type==".jpeg" || type==".png"){
intent.setDataAndType(path, "image/*");
}else{
if(type==".pdf"){
intent.setDataAndType(path, "application/pdf");
}else{
intent.setData(path);
}
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try{
AssignedTasksDetailActivity.this.startActivity(intent);
cancel();
}catch(ActivityNotFoundException e){
Toast.makeText(AssignedTasksDetailActivity.this, "No hay una aplicacion instalada para abrir este tipo de archivos", Toast.LENGTH_SHORT).show();
cancel();
}
}
});
alertDialogBuilder.show();
}
}
}
我附上了我的 fileprovider 配置,看看这是否与它有关:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.coop.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
及其xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
<root-path name="root" path="." />
</paths>
尝试查看您尝试使用 toast 打开的文件的路径,这就是让我感到震惊的地方:“内容://com.example.coop.fileprovider/root/Download/file_name.extension”。
我该如何解决?
提前致谢。