我正在尝试将文件从网络保存到我的 Android 设备,但我不知道该怎么做。 URL 地址会打开一个空白页面和一个弹出下载框,其中包含我要下载的文件,以便我可以选择保存位置。
我的问题是我不知道如何管理该框以下载文件。我不想下载该页面上显示的内容,因为该页面是空白的。所以我不能使用这段代码:
Context context = thisClass.this;
Drawable image = ImageOperations(context,
"http://android.okhelp.cz/images/adictionary/ad_4.png"
,"image.jpg");
ImageView imgView;
imgView = (ImageView)findViewById(R.id.idImageView);
imgView.setImageDrawable(image);
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
如果有人能指出我从哪里开始寻找或给我一个解决方案,我将不胜感激。
我解决了!
File card2= Environment.getExternalStorageDirectory();
File dir2= new File (card2.getAbsolutePath()+"/MyFiles");
dir2.mkdirs();
new DefaultHttpClient().execute(new HttpGet(your_url_here-this_is_a_string)).getEntity().writeTo(
new FileOutputStream(new File(dir2,your_file_on_sdcard-this_is_string)));
这也被 try/catch 包围。请记住在地址之前插入协议,否则将无法工作!
希望能帮助到你!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)