毕加索 - 内存不足的字节分配

时间:2015-08-06 07:10:38

标签: android out-of-memory picasso bitmapfactory

我使用Picassoload SD Card image进入ImageView,但有几次我遇到Out of memory问题,我可以知道我失踪的地方吗?我的代码中没有实现的内容?

08-06 12:28:52.410: E/dalvikvm-heap(15045): Out of memory on a 5591056-byte allocation.
08-06 12:28:52.410: I/dalvikvm(15045): "main" prio=5 tid=1 RUNNABLE
08-06 12:28:52.410: I/dalvikvm(15045):   | group="main" sCount=0 dsCount=0 obj=0x4207a508 self=0x4206a788
08-06 12:28:52.410: I/dalvikvm(15045):   | sysTid=15045 nice=0 sched=0/0 cgrp=apps handle=1075240752
08-06 12:28:52.415: I/dalvikvm(15045):   | schedstat=( 8555104009 1960297030 25263 ) utm=634 stm=221 core=3
08-06 12:28:52.415: I/dalvikvm(15045):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-06 12:28:52.415: I/dalvikvm(15045):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.content.res.Resources.loadDrawable(Resources.java:1963)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.content.res.Resources.getDrawable(Resources.java:672)
08-06 12:28:52.420: I/dalvikvm(15045):   at com.squareup.picasso.RequestCreator.getPlaceholderDrawable(RequestCreator.java:676)
08-06 12:28:52.420: I/dalvikvm(15045):   at com.squareup.picasso.RequestCreator.into(RequestCreator.java:637)
08-06 12:28:52.420: I/dalvikvm(15045):   at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
08-06 12:28:52.420: I/dalvikvm(15045):   at com.insurance.UploadActivity.onCreate(UploadActivity.java:101)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.Activity.performCreate(Activity.java:5191)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.os.Looper.loop(Looper.java:137)
08-06 12:28:52.420: I/dalvikvm(15045):   at android.app.ActivityThread.main(ActivityThread.java:4921)
08-06 12:28:52.420: I/dalvikvm(15045):   at java.lang.reflect.Method.invokeNative(Native Method)
08-06 12:28:52.420: I/dalvikvm(15045):   at java.lang.reflect.Method.invoke(Method.java:511)
08-06 12:28:52.420: I/dalvikvm(15045):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
08-06 12:28:52.420: I/dalvikvm(15045):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
08-06 12:28:52.420: I/dalvikvm(15045):   at dalvik.system.NativeStart.main(Native Method)

这是我的实施:

Picasso.with(UploadActivity.this)
.load(new File(filename))
.fit().centerInside()
.error(R.drawable.ic_launcher)
.placeholder(R.drawable.ic_launcher)
.into(imgPreview);

2 个答案:

答案 0 :(得分:0)

在现有代码中尝试此操作。放置首选宽度,高度和尺寸。

...
.transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT))
.resize(size, size)
...

希望这会有所帮助:)

答案 1 :(得分:0)

@Ye Min Htut的答案中BitmapTransform类的代码是:

import android.graphics.Bitmap;
import com.squareup.picasso.Transformation;

/**
 * Transformate the loaded image to avoid OutOfMemoryException
 */
public class BitmapTransform implements Transformation {

    private final int maxWidth;
    private final int maxHeight;

    public BitmapTransform(int maxWidth, int maxHeight) {
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
    }

    @Override
    public Bitmap transform(Bitmap source) {
        int targetWidth, targetHeight;
        double aspectRatio;

        if (source.getWidth() > source.getHeight()) {
            targetWidth = maxWidth;
            aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            targetHeight = (int) (targetWidth * aspectRatio);
        } else {
            targetHeight = maxHeight;
            aspectRatio = (double) source.getWidth() / (double) source.getHeight();
            targetWidth = (int) (targetHeight * aspectRatio);
        }

        Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
        if (result != source) {
            source.recycle();
        }
        return result;
    }

    @Override
    public String key() {
        return maxWidth + "x" + maxHeight;
    }

}