disable browser while android using download option
I am actually looking for a solution posted here
I specifically need to use API 8 which does not come with th开发者_StackOverflow中文版e download Manager. The code that I am using is this :
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setType(MIME_TYPE_PDF);
browserIntent.setData(Uri.parse(url));
startActivity(browserIntent);
But the browser is coming up everytime i download a file, I want to disable the browser activity. Any ideas please
Bhavya
Instead of using the browser or the (non-existent in your case) download manager, why not write your own download code. It has the side effect of using a lot less system overhead, since you are not launching a separate app to do the download.
Here's an example that should be close to what you want. f
is a File object initialized to a path on the SD card. buffer_size
and bytes
are class fields.
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 ) { ; }
}
精彩评论