我从服务器下载图像并存储在SD卡中但是当我将图像从SD卡加载到imageview时它给出了堆memory.i也尝试使用picasso库,如下面但同样给堆。像使用picaso
我从服务器下载并存储在SD卡中的图像网址,例如因为我需要离线显示图像以及
https://secure.peakmedia.at/studio/img/thumbnails/original/Gewalt-910.jpg
UsingPicasso
String path="/mnt/sdcard/peakmedia/201411031235020952.jpg";
path=path.replaceAll(" ", "\\ ");
Uri uri = Uri.fromFile(new File(path));
Picasso.with(TestActivity.this).load(uri).config(Config.RGB_565).into(imageview);
TestActivity.java
public class TestActivity extends Activity {
ImageView imageview;
int nwidth;
int nheight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
DisplayMetrics metrics=this.getResources().getDisplayMetrics();
nwidth=metrics.widthPixels;
nheight=metrics.heightPixels;
new SetImage().execute("/mnt/sdcard/peakmedia/201411031235020952.jpg");
}
public static Bitmap loadnewresize( String filename, int width, int height, boolean exact){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename,options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Config.RGB_565;
options.inDither = true;
return BitmapFactory.decodeFile(filename,options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
private class SetImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap=loadnewresize(params[0], nwidth, nheight, false);
return bitmap ;
}
@Override
protected void onPostExecute(Bitmap result) {
imageview.setImageBitmap(result);
}
}
答案 0 :(得分:2)
为了更好的表现;如果您阅读Koush在G +上发布的this帖子,您将获得清晰的解决方案,我已经提出了这个问题的总结,因为Android-Universal-Image-Loader是您需求的赢家!
Picasso 拥有最好的图片API!
UrlImageViewHelper + AndroidAsync 是最快的。玩这些 其他两个很棒的库真的突出了图像API 但是,已经过时了。
Volley 是光滑的;我非常喜欢他们的可插拔后端传输,
并可能最终放弃AndroidAsync。请求优先权
和取消管理很棒(如果你使用网络)
Android-Universal-Image-Loader 是最受欢迎的人之一 目前。高度可定制。
该项目旨在为异步提供可重复使用的仪器 图像加载,缓存和显示。它最初基于Fedor Vlasov的项目自那以后经过了大量的重构和改进 然后
考虑所有这些Android-Universal-Image-Loader套件您的要求(将图像加载到本地磁盘上)!