Android:内存不足
我正在构建一个包含图像的音乐应用程序,我正在使用毕加索加载图像。我对毕加索使用的目标如下:Android:内存不足,android,memory,picasso,android-bitmap,Android,Memory,Picasso,Android Bitmap,我正在构建一个包含图像的音乐应用程序,我正在使用毕加索加载图像。我对毕加索使用的目标如下: Target target = new Target() { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { try { if (artNormal != null) { artNormal.recycle
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
if (artNormal != null) {
artNormal.recycle();
artNormal = null;
}
artNormal = Bitmap.createBitmap(bitmap);
TransitionDrawable td = null;
if (upNextShowing) {
Bitmap scaled = Bitmap.createScaledBitmap(artNormal, 10, 10, true);
td = new TransitionDrawable(new Drawable[]{
playerArt.getDrawable(),
new BitmapDrawable(context.getResources(), scaled)
});
// Updated Part -- Updated Part
if(scaled!=null){
scaled.recycle();
scaled = null;
}
} else {
td = new TransitionDrawable(new Drawable[]{
playerArt.getDrawable(),
new BitmapDrawable(context.getResources(), bitmap)
});
}
td.setCrossFadeEnabled(true);
playerArt.setImageDrawable(td);
td.startTransition(1000);
new GetBlurredAlbumArt(artNormal).execute();
} catch (Exception e) {
e.printStackTrace();
Log.e("LargePlayer", "Error :" + e.getLocalizedMessage());
Log.e("LargePlayer", "Error :" + e.getCause());
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("LargePlayer", "Picasso Load Failed");
TransitionDrawable td = null;
if (artNormal != null) {
artNormal.recycle();
artNormal = null;
}
artNormal = BitmapFactory.decodeResource(context.getResources(),
R.drawable.album_art_large);
artNormal = Bitmap.createScaledBitmap(artNormal, 800, 800, true);
if (upNextShowing) {
Bitmap scaled = Bitmap.createScaledBitmap(artNormal, 10, 10, true);
td = new TransitionDrawable(new Drawable[]{
playerArt.getDrawable(),
new BitmapDrawable(context.getResources(), scaled)
});
// Updated Part -- Updated Part
if(scaled!=null){
scaled.recycle();
scaled = null;
}
} else {
td = new TransitionDrawable(new Drawable[]{
playerArt.getDrawable(),
new BitmapDrawable(context.getResources(), BitmapFactory.decodeResource(context.getResources(),
R.drawable.album_art_large))
});
}
td.setCrossFadeEnabled(true);
playerArt.setImageDrawable(td);
td.startTransition(1000);
new GetBlurredAlbumArt(artNormal).execute();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
class GetBlurredAlbumArt extends AsyncTask<Void, Void, Bitmap> {
Bitmap bitmap;
public GetBlurredAlbumArt(Bitmap bitmap) {
this.bitmap = bitmap;
}
@Override
protected Bitmap doInBackground(Void... params) {
bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, true);
bitmap = ImageUtilties.fastblur(bitmap, 100);
try {
return bitmap;
} catch (Exception e) {
Log.e("LargePlayer", "Error :" + e.getLocalizedMessage());
Log.e("LargePlayer", "Error :" + e.getCause());
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
TransitionDrawable td = new TransitionDrawable(new Drawable[]{
blurredColors.getDrawable() != null ? blurredColors.getDrawable() : new ColorDrawable(Color.WHITE),
new BitmapDrawable(context.getResources(), bitmap)
});
td.setCrossFadeEnabled(true);
blurredColors.setImageDrawable(td);
td.startTransition(1000);
}
}
但是我需要减少内存使用。请帮忙 一种解决方案是尝试根据屏幕分辨率显示图像。为此,一旦应用程序启动,将宽度和高度像素存储在您的
共享参考文件中,然后使用它加载缩小版本的图像。我建议的另一个解决方案是,如果可能的话,尝试使用。它使用ashmem缓存,可以存储大量数据。我个人曾与毕加索
一起面对过这个问题,并没有找到一个优雅的解决方案,但在切换到Fresco之后,所有的OOM错误都消失了。当你解码第二个位图时,第一个位图并没有被GC'ed,因为你制作了很多位图副本,你需要位图尽快使用的空闲内存和循环
看看这里,我想链接是关于加载时位图的缩放。但是毕加索使用resize()调整图像本身的大小。你是说毕加索在将图像载入内存后会调整其大小?壁画是否可以免费用于商业用途?我想是的。它绝对是开源的。但是如果你有任何问题,一定要问他们。我会尝试一下,但我的主要目标不是增加缓存大小,而是降低总体内存使用率。我没有明白你的意思。无论何时毕加索成功地加载了一幅图像,它都会转到函数onBitmapLoaded(),在该函数中,如果不为空,我将循环使用位图。我会检查您是否复制或解码文件或缩放以生成新的位图,并在完成后不循环使用它们。即使找到范围,位图是否仍保留在内存中?是,这通常是使用bitmapI时内存泄漏的原因,它回收了代码中更新的缩放位图,但仍然没有多大效果。AysncTask中的位图图像,每当我在post execute中回收它时,它都会给出一个错误“试图在回收的画布上绘制”。
android:largeHeap="true"