英文:
How to open file via intent right after downloading?
问题
以下是您要翻译的代码部分:
我需要通过我的应用外的任何相关活动打开已下载的文件。
我通过 `DownloadManager` 下载文件到默认文件夹 `Environment.DIRECTORY_DOWNLOADS`。
我在我的活动中注册了 `BroadcastReceiver`,它应该在下载完成后通过意图打开文件:
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setDataAndType(Uri.parse(uriString), getMimeType(uriString));
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(openIntent, "选择应用程序"));
}
}
}
}
}
但是在下载完成后,我遇到了一个异常:
java.lang.RuntimeException: 接收广播意图时出错
由 android.os.FileUriExposedException 引发:file:///storage/emulated/0/Download/sample-20.pdf 超出应用程序范围,通过 ClipData.Item.getUri() 公开
有什么问题?
英文:
I need to open downloaded file by any relevant activity outside my app.
I download file via DownloadManager
to default folder Environment.DIRECTORY_DOWNLOADS
I registered BroadcastReceiver
in my activity, which shall to open file right after download via intent:
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setDataAndType(Uri.parse(uriString), getMimeType(uriString));
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(openIntent, "Выберите приложение"));
}
}
}
}
}
But right after downloading I got an exception:
java.lang.RuntimeException: Error receiving broadcast Intent
caused by android.os.FileUriExposedException: file:///storage/emulated/0/Download/sample-20.pdf exposed beyond app through ClipData.Item.getUri()
What's wrong?
答案1
得分: 1
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Intent openIntent = new Intent(Intent.ACTION_VIEW);
File file = new File(URI.create(uriString));
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
openIntent.setDataAndType(uri, getMimeType(uriString));
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(openIntent, "选择应用"));
}
}
}
}
}
String getMimeType(String path) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
英文:
Correct way to open downloaded file with FileProvider
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
if (downloadManager != null) {
Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Intent openIntent = new Intent(Intent.ACTION_VIEW);
File file = new File(URI.create(uriString));
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
openIntent.setDataAndType(uri, getMimeType(uriString));
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(openIntent, "Выберите приложение"));
}
}
}
}
}
String getMimeType(String path) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论