我正在尝试从imageurl下载图像从该网址传来的图像是一个高分辨率的图像。当我尝试将此图像加载到mdpi的模拟器中时,它在所有高分辨率手机中工作正常内存泄漏异常。
我如何处理这种情况,我想在每个屏幕上都有这个图像,所以我将位图声明为全局变量
有没有办法在下载时缩小图像尺寸。我使用以下代码下载图像
c1是对图像视图的引用
bitmap = BitmapFactory.decodeStream((InputStream)new URL(logourltop.get(0)).getContent());
cl.setImageBitmap(bitmap) ;
(或)
最好在需要时使用urlimagehelper项目下载图像
UrlImageViewHelper.setUrlDrawable(cl,logourltop.get(0));
还有一个疑问是我正在使用
更改同一活动中的视图setContentView(R.layout.filename).
如果我更改了listitem上的视图,那么将释放为该位图分配的内存。(为该视图分配给对象和位图的内存)
你可以建议我一个更好的方法来避免内存泄漏。答案 0 :(得分:1)
您基本上必须以流或字节数组的形式下载图像数据和图像数据,您可以使用BitmapFactory中的便捷方法,它会为您提供一个位图,并且这个Bitmap在手中,你已经可以直接将其设置为ImageView。
确保您在分离的线程中从网络执行下载,例如使用AsyncTask .doInBackground()并在UI线程上的ImageView上设置位图,例如通过在AsyncTask的方法onPostExecute()上设置它或调用Activity.runOnUIThread()。
答案 1 :(得分:0)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class downloadimg extends Activity {
//
private ImageView mImageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Find the reference to the ImageView
mImageView = (ImageView) findViewById(R.id.test_image);
// You can set a temporary background here
//image.setImageResource(null);
// Start the DownloadImage task with the given url
new DownloadImage().execute("http://demo.imgur.com/CQzlM.jpg");
}
/**
* Simple functin to set a Drawable to the image View
* @param drawable
*/
private void setImage(Drawable drawable)
{
mImageView.setBackgroundDrawable(drawable);
}
public class DownloadImage extends AsyncTask<String, Integer, Drawable> {
@Override
protected Drawable doInBackground(String... arg0) {
// This is done in a background thread
return downloadImage(arg0[0]);
}
/**
* Called after the image has been downloaded
* -> this calls a function on the main thread again
*/
protected void onPostExecute(Drawable image)
{
setImage(image);
}
/**
* Actually download the Image from the _url
* @param _url
* @return
*/
private Drawable downloadImage(String _url)
{
//Prepare to download image
URL url;
BufferedOutputStream out;
InputStream in;
BufferedInputStream buf;
//BufferedInputStream buf;
try {
url = new URL(_url);
in = url.openStream();
// Read the inputstream
buf = new BufferedInputStream(in);
// Convert the BufferedInputStream to a Bitmap
Bitmap bMap = BitmapFactory.decodeStream(buf);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
return new BitmapDrawable(bMap);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
return null;
}
}
}
答案 2 :(得分:-1)
希望这会有所帮助......
public class BitmapResizer {
public static Bitmap decodeFile(File f,int requiredSize){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=requiredSize;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}}