Android的下载管理器API - 下载后打开文件? [英] Android DownloadManager API - opening file after download?

查看:317
本文介绍了Android的下载管理器API - 下载后打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对着通过下载管理器API全成下载后打开下载的文件的问题。在我的code:

I am facing problem of opening downloaded file after successfull download via DownloadManager API. In my code:

Uri uri=Uri.parse("http://www.nasa.gov/images/content/206402main_jsc2007e113280_hires.jpg");

    Environment
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
        .mkdirs();

    lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
                                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                                                                DownloadManager.Request.NETWORK_MOBILE)
                                .setAllowedOverRoaming(false)
                                .setTitle("app update")
                                .setDescription("New version 1.1")
                                .setShowRunningNotification(true)
                                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "a.apk")); 
    Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));

        if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))==8){
            try {
                mgr.openDownloadedFile(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("MGR", "Error");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("MGR", "Error");
            }
        }

问题是,当是,如果(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))== 8)诱发。我得到了状态-1和异常。有没有更好的办法,如何打开与下载管理器API下载的文件?在我的例子我下载大图,在现实情况下,我会下载APK文件,我需要显示isntallation UDPATE后立即对话框。

Problem is when is if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))==8) evoked. I got status -1 and exception. Is there any better way, how to open downloaded files with DownloadManager API? In my example I am downloading big image, in real situation I would be downloading APK file and I need to display isntallation dialog immediately after udpate.

编辑:我想通了,状态= 8是全成下载后。你可能有不同的检查全成下载的方式

I figured out that status=8 is after successfull download. You might have different "checking successfull download" approach

感谢

推荐答案

您需要注册一个reciever因为当下载完成:

You need to register a reciever for when the download is complete:

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

和一个BroadcastReciever处理程序

and a BroadcastReciever handler

BroadcastReceiver onComplete=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        // Do Something
    }
};

我买的而不是炸飞的一切,我建议你检查<一href="https://github.com/commonsguy/cw-android/blob/master/Internet/Download/src/com/commonsware/android/download/DownloadDemo.java">this出。

编辑:

正如一个建议,我不建议使用的API 9只是尚未: http://developer.android.com/resources/dashboard/platform-versions.html

Just as a suggestion, I wouldn't recommend using API 9 just yet: http://developer.android.com/resources/dashboard/platform-versions.html

有办法解决这个,通过创建你自己的下载处理程序,像我一样,因为我们不想疏远我们的大多数Android的用户群,为您需要: 创建 AsyncTask的它处理文件下载。

There are ways around this, by creating your very own download handler, like I did, because we didn't want to alienate most of our android's user base, for that you'll need: Create AsyncTask which handles the file download.

和我会建议建立某种形式的下载对话框(如果你说这是一个大的文件,我会让它出现在通知区域)。

and i'll recommend to create a download dialog of some sort (if you say it's a big file, i'd make it appear in the notification area).

和比你需要处理文件的打开:

and than you'll need to handle the opening of the file:

protected void openFile(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),
            "MIME-TYPE");
    startActivity(install);
}

这篇关于Android的下载管理器API - 下载后打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆