Android 从我的应用程序文件目录中打开另一个应用程序中的文件
您好,我正在使用下载管理器将一个文件从URL下载到我的文件目录,然后尝试使用操作\查看意图打开它。当我运行Action_视图时,它允许我选择quick office,但显示无法打开文件。但是,当我从通知栏单击文件时,它可以正常打开。有人知道如何从应用程序正确打开文件吗?我试图实现的是从url下载一个文件,并在下载完成后进行检测,允许用户选择在哪个应用程序中打开它 这是我到目前为止试过的Android 从我的应用程序文件目录中打开另一个应用程序中的文件,android,android-intent,broadcastreceiver,Android,Android Intent,Broadcastreceiver,您好,我正在使用下载管理器将一个文件从URL下载到我的文件目录,然后尝试使用操作\查看意图打开它。当我运行Action_视图时,它允许我选择quick office,但显示无法打开文件。但是,当我从通知栏单击文件时,它可以正常打开。有人知道如何从应用程序正确打开文件吗?我试图实现的是从url下载一个文件,并在下载完成后进行检测,允许用户选择在哪个应用程序中打开它 这是我到目前为止试过的 DownloadManager.Request request = new DownloadManager.R
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(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);
}
ContextWrapper c = new ContextWrapper(getBaseContext());
final String filePath = c.getFilesDir().getPath() + "/";
Log.v("Search", "filePath = " + filePath);
request.setDestinationInExternalFilesDir(getBaseContext(), filePath, filename);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Toast.makeText(getBaseContext(), "Downloading...", Toast.LENGTH_LONG).show();
BroadcastReceiver onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
openFile(filename);
}
protected void openFile(String fileName) {
String path = getFilesDir().getPath() +"/" + fileName;
File f = new File(path);
String Extension = Global.getFileExt(fileName);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
String mimeType = myMime.getMimeTypeFromExtension(Extension);
try {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
intent.setDataAndType(Uri.fromFile(file),mimeType);
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(getBaseContext(), "No handler for this type of file.", 4000).show();
}
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
} else{
}
return false;
}else{
view.loadUrl(url);
return true;
}
}
看看我对这个问题的回答,我认为这几乎是同一个问题: