gpt4 book ai didi

java - android 2.3 出现内存泄漏错误

转载 作者:行者123 更新时间:2023-12-01 14:26:26 24 4
gpt4 key购买 nike

我最近遇到了内存泄漏错误,我想做的是创建一个带有画廊的 Activity ,从服务下载图片并显示它们,第一次出现错误时,我做了一些调查,发现这要么是上下文的引用,要么是不同版本的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)

最佳答案

这里的问题本身并不是内存泄漏,真正的问题是异常所指向的问题:

OutOfMemoryError: bitmap size exceeds VM budget

检查引发异常的行,您会发现它与正在加载的图像有关。该图像太大,处理它所消耗的内存不足以完成任务。

您可以在 this other answer 上找到很好的引用资料。一个好的方法是使用 options BitmapFactory的类和decode仅当您找到可以防止 OutOfMemory 的正确采样时才显示图像问题发生。

关于java - android 2.3 出现内存泄漏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17150549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com