我正在構建一個帶有圖像的音樂應用程序,我正在使用Picasso加載圖像。我在畢加索使用的目標如下:Android:內存不足
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);
}
}
每次圖像更改時,內存消耗增加2-3 MB。內存使用量可能增加到200 MB(這肯定是不可接受的)。活動中只有2個圖像視圖發生變化。圖像尺寸接近1200x1200。我知道它們非常大,但是我在每次設置之前都會循環使用位圖,但內存使用情況仍然會像任何情況一樣增加。該應用程序崩潰後。它也有時會給出錯誤嘗試使用回收的位圖。也試過
android:largeHeap="true"
但我需要減少內存使用量。請幫忙!
採取這裏看看[鏈接](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-位圖對象) –
我想這個鏈接是關於加載時縮放位圖的。但畢加索使用resize()調整圖像本身。你是說畢加索在加載到內存後調整圖像大小? –