我最近遇到了内存泄漏错误,我正在尝试创建一个带有库的活动,该库从服务下载图片并显示它们,第一次出现错误,我做了一些调查,结果证明它是上下文的引用,或不同版本的android管理内存中的图像的方式。我对代码进行了一些更改,最后结果是这样的:
public class GalleryView extends Activity {
private LinearLayout imageView;
private String GalleryId;
private ArrayList<Drawable> images = new ArrayList<Drawable>();
private ArrayList<Drawable> thumbs = new ArrayList<Drawable>();
private ArrayList<String> urls;
private AdvertiserDAO aDao = new AdvertiserDAO();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galeria);
setGallery();
setThumbs();
Button back = (Button) findViewById(R.id.Back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
Gallery ga = (Gallery) findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (LinearLayout) findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
imageView.removeAllViews();
} catch (Exception e) {
e.getMessage();
}
TouchImageView touchImageView = new TouchImageView(
GalleryView.this);
touchImageView.setImageDrawable(images.get(arg2));
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
imageView.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
touchImageView.setLayoutParams(lp);
imageView.addView(touchImageView);
}
});
}
private void setGallery() {
SharedPreferences sp = getSharedPreferences("galeriaId", 0);
GalleryId = sp.getString("galeriaId", null);
//retrieves urls of the pictures of the gallery
urls = aDao.getUrls(GalleryId);
for (int i = 0; i < urls.size(); i++) {
Drawable d = ImageOperations(urls.get(i), "image" + i
+ ".jpg");
images.add(d);
}
}
private void setThumbs() {
SharedPreferences sp = getSharedPreferences("galeriaId", 0);
GalleryId = sp.getString("galeriaId", null);
//retrieves the urls of the thumbs of the gallery
urls = aDao.getThumbs(GalleryId);
for (int i = 0; i < urls.size(); i++) {
Drawable d = ImageOperations( urls.get(i), "image" + i
+ ".jpg");
thumbs.add(d);
}
}
private Drawable ImageOperations(String url,
String saveFilename) {
InputStream is = (InputStream) fetch(url);
Drawable d = null;
try {
d = new ImageOperationsAsyncTask().execute(is).get();
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
} catch (ExecutionException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
return d;
}
public Object fetch(String address) {
Object content = null;
try {
content = new URLAsyncTAsk().execute(address).get();
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
} catch (ExecutionException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
return content;
}
private class ImageOperationsAsyncTask extends
AsyncTask<InputStream, Integer, Drawable> {
@Override
protected Drawable doInBackground(InputStream... params) {
InputStream is = null;
for(InputStream i: params){
is = i;
}
Drawable result = Drawable.createFromStream(is, "src");
return result;
}
}
private class URLAsyncTAsk extends AsyncTask<String, Integer, Object> {
@Override
protected Object doInBackground(String... params) {
String address = "";
for (String s : params) {
address = s;
}
URL url;
Object content = null;
try {
url = new URL(address);
content = url.getContent();
} catch (MalformedURLException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
return content;
}
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return images.size();
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageDrawable(thumbs.get(arg0));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}}
此代码适用于我的Android 4.1手机,以及Android 2.2模拟器,但在Android 2.3模拟器和手机上它仍然在日志猫中显示内存泄漏错误。
任何想法为什么它只在android 2.3 ???
中起作用编辑: 这是LogCat发布的日志:
06-17 14:41:45.050: W/System.err(305): java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-17 14:41:45.060: W/System.err(305): at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:224)
06-17 14:41:45.060: W/System.err(305): at java.util.concurrent.FutureTask.get(FutureTask.java:83)
06-17 14:41:45.060: W/System.err(305): at android.os.AsyncTask.get(AsyncTask.java:340)
06-17 14:41:45.060: W/System.err(305): at directorio.actividades.GalleryView.ImageOperations(GalleryView.java:140)
06-17 14:41:45.060: W/System.err(305): at directorio.actividades.GalleryView.setGallery(GalleryView.java:107)
06-17 14:41:45.060: W/System.err(305): at directorio.actividades.GalleryView.onCreate(GalleryView.java:50)
06-17 14:41:45.060: W/System.err(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-17 14:41:45.060: W/System.err(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-17 14:41:45.060: W/System.err(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-17 14:41:45.060: W/System.err(305): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-17 14:41:45.060: W/System.err(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-17 14:41:45.060: W/System.err(305): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 14:41:45.060: W/System.err(305): at android.os.Looper.loop(Looper.java:130)
06-17 14:41:45.060: W/System.err(305): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-17 14:41:45.060: W/System.err(305): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 14:41:45.060: W/System.err(305): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 14:41:45.060: W/System.err(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-17 14:41:45.060: W/System.err(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-17 14:41:45.060: W/System.err(305): at dalvik.system.NativeStart.main(Native Method)
06-17 14:41:45.060: W/System.err(305): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-17 14:41:45.060: W/System.err(305): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-17 14:41:45.060: W/System.err(305): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
06-17 14:41:45.060: W/System.err(305): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
06-17 14:41:45.060: W/System.err(305): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-17 14:41:45.060: W/System.err(305): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
06-17 14:41:45.060: W/System.err(305): at directorio.actividades.GalleryView$ImageOperationsAsyncTask.doInBackground(GalleryView.java:195)
答案 0 :(得分:1)
这里的问题不是内存泄漏本身,真正的问题是异常指向的问题:
OutOfMemoryError:位图大小超过VM预算
检查抛出异常的行,您会发现它与正在加载的图像有关。该图像太大,处理它所消耗的内存不足以完成任务。
您可以在this other answer找到一个很好的参考。一个好方法是,只有当您找到可以防止options
问题发生的正确采样时,才能使用BitmapFactory
类的decode
和OutOfMemory
图像。< / p>