当我在一个活动中从gallary加载两个图像时内存不足发出位图

时间:2015-09-17 12:43:12

标签: android bitmap

我正在开发一个照相框的应用程序。用户可以在一帧中从gallary中选择两个图像。但每当我从gallary设置任何一个图像,然后去选择其他图像应用程序崩溃,并在日志中显示错误消息内存不足。 我如何删除此错误

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == 10 && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();


                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                // Set the Image in ImageView after decoding the String


                imageView1.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));


            }
            if (requestCode == 100 && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();


                // Set the Image in ImageView after decoding the String
               // 
                imageView2.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));

            }
  }

请帮帮我

2 个答案:

答案 0 :(得分:1)

使用Universal ImageLoaderVolley Library加载图片。它将使您的应用程序保持平稳,不会出现内存不足问题。

答案 1 :(得分:0)

您好请尝试以下代码希望它能帮到您

public static Bitmap decodeFile(File file, int width, int height)
    {
        try
        {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null, o);
            int scale = 1;

            //The new size we want to scale to
            final int REQUIRED_WIDTH = width;
            final int REQUIRED_HIGHT = height;



            //Find the correct scale value. It should be the power of 2.
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
            {
                scale *= 2;
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
        }
        catch (FileNotFoundException e)
        {

        }
        return null;
    }

请使用Android-Collage-View library。它符合您的要求