Android 安卓:Can';t多次加载位图图像

Android 安卓:Can';t多次加载位图图像,android,bitmap,out-of-memory,Android,Bitmap,Out Of Memory,我读了很多关于android上位图的帖子,找不到任何适合我代码的解释。 我正在SD卡中保存相机的图像,而当我试图显示所有图像时,我会收到下面的错误提示 06-09 20:38:03.732: E/AndroidRuntime(3657): java.lang.OutOfMemoryError 06-09 20:38:03.732: E/AndroidRuntime(3657): at android.graphics.BitmapFactory.nativeDecodeStream(Na

我读了很多关于android上位图的帖子,找不到任何适合我代码的解释。 我正在SD卡中保存相机的图像,而当我试图显示所有图像时,我会收到下面的错误提示

06-09 20:38:03.732: E/AndroidRuntime(3657): java.lang.OutOfMemoryError
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at br.com.rigolas.financial.activity.InsertItem.getBitmap(InsertItem.java:415)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at br.com.rigolas.financial.activity.InsertItem.showAttachment(InsertItem.java:194)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at br.com.rigolas.financial.activity.InsertItem.populateFields(InsertItem.java:184)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at br.com.rigolas.financial.activity.InsertItem.onResume(InsertItem.java:144)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1188)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.Activity.performResume(Activity.java:5280)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.os.Looper.loop(Looper.java:137)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at android.app.ActivityThread.main(ActivityThread.java:4898)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at java.lang.reflect.Method.invoke(Method.java:511)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-09 20:38:03.732: E/AndroidRuntime(3657):     at dalvik.system.NativeStart.main(Native Method)
我的代码:

showAttachment(atts);
其中atts是一个阵列操作系统路径

private void showAttachment(List<Attachment> atts) {

    for(Attachment att : atts){
        insertPhoto(getBitmap(att.getPath()));
    }

}

public Bitmap getBitmap(String path){

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);

    return bitmap;

}

public void insertPhoto(Bitmap btmp){
    final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
            R.layout.photo_item, mContainerView, false);


    newView.setBackground(new BitmapDrawable(getResources(),btmp));
    ImageView delete = (ImageView) newView.findViewById(R.id.photo_expense_discart);
    delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //mContainerView.removeView(newView);
            Intent imageDetil = new Intent(InsertItem.this,ImageDetailActivity.class);
            InsertItem.this.startActivity(imageDetil);
        }
    });

    mContainerView.addView(newView, 0);
}
private void showAttachment(列表附件){
附件(附件:附件){
insertPhoto(getBitmap(att.getPath());
}
}
公共位图getBitmap(字符串路径){
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
bmOptions.inJustDecodeBounds=false;
bmOptions.inpurgable=true;
位图位图=位图工厂.decodeFile(路径,bmOptions);
返回位图;
}
公共void insertPhoto(位图btmp){
最终视图组newView=(视图组)布局更平坦。从(此)。充气(
R.layout.photo_项目,mContainerView,假);
setBackground(新的BitmapDrawable(getResources(),btmp));
ImageView delete=(ImageView)newView.findViewById(R.id.photo\u expense\u discart);
delete.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//mContainerView.removeView(newView);
Intent imageDetil=新的Intent(InsertItem.this,ImageDetailActivity.class);
插入项.this.startActivity(imageDetil);
}
});
mContainerView.addView(newView,0);
}
和我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_margin="5dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:alpha="80"
    android:background="#80000000"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/photo_expense_discart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_content_discard_w" />
</LinearLayout>


如果我用一个图像来显示,效果很好,但是当我尝试显示多个图像时,我会出现错误,有人知道我如何解决这个问题吗?谢谢查看您提供的这段代码:

public Bitmap getBitmap(String path){

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);

    return bitmap;

}

Bitmap Bitmap=BitmapFactory.decodeFile(路径,bmOptions)行中,您可能试图解码过大的文件,导致outofmemory异常。

这篇文章应该回答您的问题: