Google Map自定义标记内存不足错误(API V2)

时间:2013-06-02 20:31:31

标签: android google-maps google-maps-markers google-maps-android-api-2

我正在使用以下代码在他/她的图库中使用用户自己的图像设置标记。但是我总是出现内存错误,所以我猜我的实现是错误的。我发现的另一个有趣的行为是,如果标记不在视图中,则不会立即发生错误。但是,一旦我将相机移动到该标记的位置,就会再次出现错误。 (简而言之,我从来没有机会看到我的形象)

我使用的代码:

//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错误:E / dalvikvm-heap(5809):16777232字节分配时内存不足。

调试时我将picturePath更改为已知路径,例如“/mnt/sdcard/DCIM/Camera/IMG_20121214.jpg”,但错误是相同的。

提前致谢:)

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

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

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