我正在尝试将所有图像加载到图像视图中,但是在12张图像后,应用程序崩溃并出现内存不足的情况

我试图回收位图,但由于无法重用位图画布而失败

这是我的代码

public void LoadImageArray() {
        Bitmap mybitmap;
        LinearLayout layout = (LinearLayout) findViewById(R.id.image_container);
        //LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
        String path= Environment.getExternalStorageDirectory().toString()+"/Pictures";
        File f=new File(path);
        File file[]=f.listFiles();
        BitmapFactory.Options options=new BitmapFactory.Options();
        for (int i = 0; i < 13; i++) {
            layoutParams.setMargins(1, 1, 1, 1);
            layoutParams.gravity = Gravity.LEFT;
            ImageView imageView = new ImageView(this);

            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;

            String imageType=options.outMimeType;
            if(imageWidth > imageHeight) {
                options.inSampleSize = calculateInSampleSize(options,100,100);//if landscape
            } else{
                options.inSampleSize = calculateInSampleSize(options,100,100);//if portrait
            }
            options.inJustDecodeBounds = false;
            //Bitmap
            try {
                File efile=new File(file[i].getAbsolutePath());
                if(efile.exists()) {
                    mybitmap = BitmapFactory.decodeFile(file[i].getAbsolutePath(), options);


                    imageView.setLayoutParams(layoutParams);
                    imageView.setImageBitmap(mybitmap);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(1, 1, 1, 1);
                    imageView.setColorFilter(34);
                    imageView.setClickable(true);

                    if (mybitmap != null) {
                        layout.addView(imageView);
                       // if (!((BitmapDrawable) imageView.getDrawable()).getBitmap().isRecycled()) {
                       //     ((BitmapDrawable) imageView.getDrawable()).getBitmap().recycle();
                      //  }
                    }
                    //else {layout.addView(imageView);}
                    Log.d("Files", file[i].getName());
                }
            } catch(Exception e) {Log.d("TAG","Error is " +e);}
        }
    }

最佳答案

您没有正确使用选项,首先创建选项,然后将inJustDecodeBounds设置为true,然后在其上调用解码文件,然后按其他方式进行操作,但是在像这样创建选项后立即使用它们

public void LoadImageArray() {
    Bitmap mybitmap;
    LinearLayout layout = (LinearLayout) findViewById(R.id.image_container);
    //LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
    String path= Environment.getExternalStorageDirectory().toString()+"/Pictures";
    File f=new File(path);
    File file[]=f.listFiles();

    for (int i = 0; i < 13; i++) {
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true;
         BitmapFactory.decodeFile(file[i].getAbsolutePath(), options);

        layoutParams.setMargins(1, 1, 1, 1);
        layoutParams.gravity = Gravity.LEFT;
        ImageView imageView = new ImageView(this);

        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;

        String imageType=options.outMimeType;
        if(imageWidth > imageHeight) {
            options.inSampleSize = calculateInSampleSize(options,100,100);//if landscape
        } else{
            options.inSampleSize = calculateInSampleSize(options,100,100);//if portrait
        }
        options.inJustDecodeBounds = false;
        //Bitmap
        try {
            File efile=new File(file[i].getAbsolutePath());
            if(efile.exists()) {
                mybitmap = BitmapFactory.decodeFile(file[i].getAbsolutePath(), options);


                imageView.setLayoutParams(layoutParams);
                imageView.setImageBitmap(mybitmap);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(1, 1, 1, 1);
                imageView.setColorFilter(34);
                imageView.setClickable(true);

                if (mybitmap != null) {
                    layout.addView(imageView);
                   // if (!((BitmapDrawable) imageView.getDrawable()).getBitmap().isRecycled()) {
                   //     ((BitmapDrawable) imageView.getDrawable()).getBitmap().recycle();
                  //  }
                }
                //else {layout.addView(imageView);}
                Log.d("Files", file[i].getName());
            }
        } catch(Exception e) {Log.d("TAG","Error is " +e);}
    }
}


标签android java imageview栏目Java

08-05 10:00
查看更多