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 :)

4

2 回答 2

5

在加载到内存之前解码和缩放图像,只需将横向和纵向更改为您实际想要的大小

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);

尺寸计算方法

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;
}
于 2013-06-02T21:22:01.157 回答
2

您正在尝试将 4 Mpix 图像作为标记图标。这似乎不是一个好主意。

将其加载为位图,将其缩小到合理的大小。

于 2013-06-02T20:55:52.960 回答