使用下载选项在Android时禁用浏览器

时间:2011-07-07 16:10:08

标签: android download android-intent

我实际上正在寻找张贴here

的解决方案

我特别需要使用下载管理器不附带的API 8。我正在使用的代码是:

        Intent browserIntent = new Intent(Intent.ACTION_VIEW);
                        browserIntent.setType(MIME_TYPE_PDF);
                        browserIntent.setData(Uri.parse(url));
                        startActivity(browserIntent); 

但每次下载文件时浏览器都会出现,我想禁用浏览器活动。 有任何想法

Bhavya

1 个答案:

答案 0 :(得分:0)

为什么不编写自己的下载代码,而不是使用浏览器或(在您的情况下不存在)下载管理器。它具有使用更少系统开销的副作用,因为您没有启动单独的应用程序来进行下载。

这是一个应该接近你想要的例子。 f是初始化为SD卡路径的File对象。 buffer_sizebytes是类字段。

private static final int buffer_size=1024;
private static final byte[] bytes=new byte[buffer_size];

InputStream is = null;
FileOutputStream os = null;

try {
    is = new URL(url).openStream();
    os = new FileOutputStream(f);

    for (;;) {
        int count = is.read( bytes, 0, buffer_size );
        if ( count == -1 ) break;
        os.write( bytes, 0, count );
    }
}
catch (Exception e) {
    Log.e( TAG, "error : " + e.getLocalizedMessage(), e );
}
finally {
    try { is.close(); } catch ( Exception ignore ) { ; }
    try { os.close(); } catch ( Exception ignore ) { ; }
}
相关问题