1

I'm using the following code to set marker with user's own image in his/her gallery. But I get out of memory error all the time so I guess my implementation is wrong. Another interesting behavior I found is that if the marker isn't in the view, the error doesn't occur immediately. But once I move the camera to where that marker is the error appears again. (In short, I never get a chance to see my image)

Codes I use:

//on button click, send user to gallery to choose image he/she wants to use
changeAvatarButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 1);
        }
    });


//use the selected image for marker icon
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
        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]);
        String picturePath = cursor.getString(columnIndex);

        cursor.close();

        // BitmapDescriptorFactory
        myIcon.setIcon(BitmapDescriptorFactory
                .fromPath(picturePath));

    }
}

logcat error: E/dalvikvm-heap(5809): Out of memory on a 16777232-byte allocation.

When debugging I change picturePath to a known path such as "/mnt/sdcard/DCIM/Camera/IMG_20121214.jpg" but the error is the same.

Thanks in advance :)

Kara
  • 6,115
  • 16
  • 50
  • 57
Arch1tect
  • 4,128
  • 10
  • 48
  • 69

2 Answers2

5

decode and scale image before loaded into memory,just change landscape and portrait to the size you actually want

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
if(imageWidth > imageHeight) {
options.inSampleSize = calculateInSampleSize(options,512,256);//if     landscape
} else{
options.inSampleSize = calculateInSampleSize(options,256,512);//if     portrait
}
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path,options);

method for calculating size

public static int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

  // Calculate ratios of height and width to requested height and     width
  final int heightRatio = Math.round((float) height / (float)     reqHeight);
  final int widthRatio = Math.round((float) width / (float) reqWidth);

  // Choose the smallest ratio as inSampleSize value, this will     guarantee
  // a final image with both dimensions larger than or equal to the
  // requested height and width.
  inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
 }

 return inSampleSize;
}
JRowan
  • 6,824
  • 8
  • 40
  • 59
  • thank you, after your code, do I just call ` mydecrptimg.setImageBitmap(bitmap);` directly? I tried this and get a null pointer exception.... – Arch1tect Jun 02 '13 at 21:44
  • MY decrptimage, what's that? – JRowan Jun 02 '13 at 21:53
  • oh, sorry, I mean `myIcon.setIcon(BitmapDescriptorFactory.fromBitmap(bmp)); ` – Arch1tect Jun 02 '13 at 21:54
  • Try to step through with the debugger and see what is happening, something is returning null, it works for me – JRowan Jun 02 '13 at 22:05
  • thanks a lot again~! Your part is correct. I tried a hard coded path then it works! Now I'm wondering why the path my code return is wrong.... – Arch1tect Jun 02 '13 at 22:09
  • It looks ok to me, hold on, I'll check – JRowan Jun 02 '13 at 22:15
  • Filepath column = { android.provider.MediaStore.MediaColumns.DATA }; try that instead – JRowan Jun 02 '13 at 22:20
  • 06-02 18:24:33.182: E/AndroidRuntime(23331): FATAL EXCEPTION: main 06-02 18:24:33.182: E/AndroidRuntime(23331): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.gallery3d.provider/picasa/item/5627752136956094418 }} to activity {com.abc.googlemaplayer/com.abc.googlemaplayer.MainActivity}: java.lang.NullPointerException – Arch1tect Jun 02 '13 at 22:26
  • so strange..I print out the image path I code which looks just right `/mnt/sdcard/DCIM/Camera/IMG_20121214.jpg`... – Arch1tect Jun 02 '13 at 22:28
  • Hold on I'll give you this link for picassa, its different – JRowan Jun 02 '13 at 22:29
  • Look at this, I never tried picassa I just know its different http://stackoverflow.com/questions/15330628/how-to-load-a-picasa-image-from-uri – JRowan Jun 02 '13 at 22:31
  • No actually this one, this one is answered my bad http://stackoverflow.com/questions/15284592/unable-to-select-particular-images-using-action-pick-intent – JRowan Jun 02 '13 at 22:35
  • wow! you are right again! For the abs path I get nothing when the image is from picassa! – Arch1tect Jun 02 '13 at 22:35
  • OK, now I find out that as long as I don't select image from picassa, it work just fine. Gonna try to solve the picassa part now. You really helped me a lot already. Appreciate it! – Arch1tect Jun 02 '13 at 22:38
2

You are trying to put 4 Mpix image as a marker icon. That doesn't seem like a good idea.

Load it as a Bitmap, scaling it down to reasonable size.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94