简体   繁体   English

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

[英]How to open file via intent right after downloading?

I need to open downloaded file by any relevant activity outside my app.我需要通过我的应用程序之外的任何相关活动打开下载的文件。

I download file via DownloadManager to default folder Environment.DIRECTORY_DOWNLOADS我通过DownloadManager将文件下载到默认文件夹Environment.DIRECTORY_DOWNLOADS

I registered BroadcastReceiver in my activity, which shall to open file right after download via intent:我在我的活动中注册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, "Выберите приложение"));
                }
            }
        }
    }
}

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?怎么了?

Correct way to open downloaded file with FileProvider使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM