如何在下载后立即通过意图打开文件?

huangapple go评论100阅读模式
英文:

How to open file via intent right after downloading?

问题

以下是您要翻译的代码部分:

  1. 我需要通过我的应用外的任何相关活动打开已下载的文件
  2. 我通过 `DownloadManager` 下载文件到默认文件夹 `Environment.DIRECTORY_DOWNLOADS`
  3. 我在我的活动中注册了 `BroadcastReceiver`它应该在下载完成后通过意图打开文件
  4. @Override
  5. public void onReceive(Context context, Intent intent) {
  6. if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
  7. long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
  8. DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
  9. if (downloadManager != null) {
  10. Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
  11. if (c.moveToFirst()) {
  12. int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
  13. if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
  14. String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  15. Intent openIntent = new Intent(Intent.ACTION_VIEW);
  16. openIntent.setDataAndType(Uri.parse(uriString), getMimeType(uriString));
  17. openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  18. context.startActivity(Intent.createChooser(openIntent, "选择应用程序"));
  19. }
  20. }
  21. }
  22. }
  23. }

但是在下载完成后,我遇到了一个异常:

  1. java.lang.RuntimeException: 接收广播意图时出错
  2. 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:

  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
  4. long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
  5. DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
  6. if (downloadManager != null) {
  7. Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
  8. if (c.moveToFirst()) {
  9. int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
  10. if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
  11. String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  12. Intent openIntent = new Intent(Intent.ACTION_VIEW);
  13. openIntent.setDataAndType(Uri.parse(uriString), getMimeType(uriString));
  14. openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  15. context.startActivity(Intent.createChooser(openIntent, "Выберите приложение"));
  16. }
  17. }
  18. }
  19. }
  20. }

But right after downloading I got an exception:

  1. java.lang.RuntimeException: Error receiving broadcast Intent
  2. 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

  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
  4. long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
  5. DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
  6. if (downloadManager != null) {
  7. Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
  8. if (c.moveToFirst()) {
  9. int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
  10. if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
  11. String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  12. Intent openIntent = new Intent(Intent.ACTION_VIEW);
  13. File file = new File(URI.create(uriString));
  14. Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
  15. openIntent.setDataAndType(uri, getMimeType(uriString));
  16. openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  17. context.startActivity(Intent.createChooser(openIntent, "选择应用"));
  18. }
  19. }
  20. }
  21. }
  22. }
  23. String getMimeType(String path) {
  24. String type = null;
  25. String extension = MimeTypeMap.getFileExtensionFromUrl(path);
  26. if (extension != null) {
  27. type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  28. }
  29. return type;
  30. }
英文:

Correct way to open downloaded file with FileProvider

  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
  4. long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
  5. DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
  6. if (downloadManager != null) {
  7. Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
  8. if (c.moveToFirst()) {
  9. int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
  10. if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
  11. String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  12. Intent openIntent = new Intent(Intent.ACTION_VIEW);
  13. File file = new File(URI.create(uriString));
  14. Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
  15. openIntent.setDataAndType(uri, getMimeType(uriString));
  16. openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  17. context.startActivity(Intent.createChooser(openIntent, "Выберите приложение"));
  18. }
  19. }
  20. }
  21. }
  22. }
  23. String getMimeType(String path) {
  24. String type = null;
  25. String extension = MimeTypeMap.getFileExtensionFromUrl(path);
  26. if (extension != null) {
  27. type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
  28. }
  29. return type;
  30. }

huangapple
  • 本文由 发表于 2020年8月13日 20:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63394816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定