0

I have 2 imageView to import images from gallery and set on these imageViews. I basically do this by:

String mPicPath1, mPicPath2;

protected void onCreate(Bundle icicle) {

mPicPath1 = null;
mPicPath2 = null; 

}

@Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
super.onActivityResult(requestCode, resultcode, data);

switch(requestCode){
case 1:
    if (data != null && resultcode == RESULT_OK) 
    {              

        Uri selectedImage = data.getData();

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        mPicPath1 = cursor.getString(columnIndex);
        cursor.close();

        logoview.setBackgroundResource(0);
        logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));
    }
break;  
case 2:
    if (data != null && resultcode == RESULT_OK) 
    {              

        Uri selectedImage = data.getData();                     
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        cursor.moveToFirst();                   
        mPicPath2 = cursor.getString(columnIndex);
        cursor.close();
        qrcodeview.setBackgroundResource(0);
        qrcodeview.setImageBitmap(BitmapFactory.decodeFile(mPicPath2));
    }
break;
}

and i use a button onClickListener to start intent and go to SecondActivity:

save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {

@Override   
public void onClick(View v) {

Intent intent = new Intent(NewCard.this, Template.class);


    if (!TextUtils.isEmpty(mPicPath1)) {
        intent.putExtra("picture_path1", PicPath1);
    }
    if (!TextUtils.isEmpty(mPicPath2)) {
       intent.putExtra("picture_path2", PicPath2);
    }
    startActivity(intent);

}
});

And my SecondActivity to set images on 2 different imageViews:

String pre_img_path1= getIntent().getStringExtra("picture_path1");
ImageView crdlogoframe = (ImageView)       findViewById(R.id.crdlogoframe);
crdlogoframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path1));

String pre_img_path2= getIntent().getStringExtra("picture_path2");
ImageView crdqrframe = (ImageView) findViewById(R.id.crdqrframe);
crdqrframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path2));

So my problem is about the file size or resolution of the images. If i take 2 high resolution images from gallery (taken by standard camera: 1992kb, 3264x2448) and i click my save (save.onClickListener) button, i receive Force Close error. If i take small size images there is no problem(74kb, 800x600) i can proceed SecondActivity and see images are set. How i can solve this issue. Should i use a syntax to resize the images when i pick or set. The formats are both .Jpeg. Thank you very much.

Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
  • 1
    The file size is not relevant. The memory used, by default, is width x height x 4 bytes. So, 3264x2488x4 = approx 31MB. You will either have to scale the image when you load it or reduce the image resolution. Plenty of questions and answers already on SO to do this. – Simon Dec 29 '13 at 16:07
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Simon Dec 29 '13 at 16:07
  • But im not using Camera intent to take picture. I just pick the taken images. Can you pls check my code and suggest me more practically. – Umit Kaya Dec 29 '13 at 16:21
  • @Simon it is 1992Kb and 2 images makes around 4MB. – Umit Kaya Dec 29 '13 at 16:33
  • Please read my comment again. The file size is not relevant. The image is compressed when it's stored and decompressed when loaded. The memory required is as I said, width x height * 4 bytes since by default, each pixel requires 4 bytes (32 bit colour depth) – Simon Dec 29 '13 at 16:45

2 Answers2

0

i do not have the rep to comment yet so .... but as Simon said your images are to large 31MB need to try and keep things under about 16MB so you will have to re-size them before you can display them

m.drz
  • 81
  • 6
  • There is no way 31MB. As i mentioned in my comment it is 1992Kb and 2 images makes around 4MB. This is large enough to not display in my app. – Umit Kaya Dec 29 '13 at 16:32
  • the 1992kb is probably the compressed file size which is not the same as the amount of memory needed to display the image. as Simon said ...again .... width X height X 4 will get you close to memory needed to display it – m.drz Dec 29 '13 at 16:46
  • Put it this way. I can create file which takes 100K on disk and needs 2 MB of RAM to display, or I can create a file which takes 2MB on disk, and needs 2MB to display. Once again, the file size is not relevant. – Simon Dec 29 '13 at 16:47
0

Instead of

BitmapFactory.decodeFile(mPicPath2)

You need to use something like

BitmapFactory.decodeFile(mPicPath2, options)

Where options is an instance of BitmapFactory.Options with an inSampleSize set to something like 4 that will scale down the image as it is loaded.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Firstly thanks, i tried but it didnt help me. Maybe it is bec of mPicPath1 and mPicPath2 is String value? – Umit Kaya Dec 29 '13 at 17:23