Google Map custom Marker Out of Memory Error (API V2)

前端 未结 2 1410
时光取名叫无心
时光取名叫无心 2021-01-23 21:04

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

相关标签:
2条回答
  • 2021-01-23 21:48

    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;
    }
    
    0 讨论(0)
  • 2021-01-23 21:53

    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.

    0 讨论(0)
提交回复
热议问题