Android 谷歌地图自定义标记内存不足错误(API V2)

Android 谷歌地图自定义标记内存不足错误(API V2),android,google-maps,google-maps-markers,google-maps-android-api-2,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.setOnClickListen

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

我使用的代码:

//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-heap5809:16777232字节分配的内存不足

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


提前感谢:

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


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

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


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

在将图像加载到内存之前,只需将横向和纵向更改为实际需要的大小即可

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

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

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

谢谢,在您的代码之后,我是否只需要调用'MyDecriptimg.setImageBitmapbitmap;`直接地我尝试了这个,得到了一个空指针异常…我的decrptimage,那是什么?哦,对不起,我的意思是myIcon.setIconBitmapDescriptorFactory.fromBitmapbmp;尝试一步一步地使用调试器,看看发生了什么,有东西返回null,它再次对甲烷非常有效~!你的部分是正确的。我尝试了一个硬编码的路径,然后它的工作!现在我想知道为什么我的代码返回的路径是错误的……谢谢,在您的代码之后,我是否只调用'mydecrptimg.setImageBitmapbitmap;'直接地我尝试了这个,得到了一个空指针异常…我的decrptimage,那是什么?哦,对不起,我的意思是myIcon.setIconBitmapDescriptorFactory.fromBitmapbmp;尝试一步一步地使用调试器,看看发生了什么,有东西返回null,它再次对甲烷非常有效~!你的部分是正确的。我尝试了一个硬编码的路径,然后它的工作!现在我想知道为什么我的代码返回的路径是错误的。。。。