我正在使用以下方法从url下载单个图像
public static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
有时我会得到一个outofmemory异常.
我无法捕捉到内存异常.该应用程序将关闭.怎么预防这个?
是否有更好的下载图像的方法也更快?
嗨,我是Android的Retrofit框架的新手.我可以使用它从REST服务获得JSON响应,但我不知道如何使用改造下载png.我想从这个网址下载png:http: //wwwns.akamai.com/media_resources/globe_emea.png.什么应该响应要在Callback <>中指定的对象来实现这一点.
我正在尝试使用API Picasso保存图像.为了做到这一点,我正在努力Target保存,但我不能做这项工作.
我怎么能这样做?
试
//save image
public static void imageDownload(Context ctx){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png"));
}
//target to save
private static Target getTarget(final String url){
Target target = new Target(){
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
//Log.i("PRODUTOS_FOLDER", CreateAppFolder.getProdutosFolder());
File file = new File(Environment.getExternalStorageDirectory() + url);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start(); …Run Code Online (Sandbox Code Playgroud)