谷歌地图自定义标记内存不足错误(API V2) [英] Google Map custom Marker Out of Memory Error (API V2)

查看:155
本文介绍了谷歌地图自定义标记内存不足错误(API V2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code设置标志在他/她的画廊用户自身的形象。但我得到了内存不足的错误所有的时间,所以我想我的实现是错误的。另一个有趣的行为我发现的是,如果该标记不处于图,该错误不会立即发生。但是,一旦我将摄像机移动到该标记为错误再次出现。 (总之,我永远不会有机会看到我的形象)

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我使用的:

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

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

在调试更改picturePath到已知路径,如/mnt/sdcard/DCIM/Camera/IMG_20121214.jpg但错误是一样的。

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

在此先感谢:)

推荐答案

德code和规模的图像之前加载到内存中,只是改变横向和纵向到你真正想要的大小

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

这篇关于谷歌地图自定义标记内存不足错误(API V2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆