Upload image to map marker using Android

I am trying to add a bitmap to the google map marker icon.

Below is my code to add an image to the marker icon. Application crash due to bitmap size

Error: java.lang.IllegalArgumentException: Textures with dimensions 4096x8192 are larger than the maximum supported size is 4096x4096

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        String imageType=options.outMimeType;
        if(imageWidth > imageHeight) {
            options.inSampleSize = calculateInSampleSize(options,10,10);//if landscape
        } else{
            options.inSampleSize = calculateInSampleSize(options,10,10);//if portrait
        }
        options.inJustDecodeBounds = false;


        Bitmap  mapbitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/TAG_IMG_25.2571724_55.2994563.jpg", options);

latLng=new LatLng(25.254376, 55.2973058);

marker = googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(_title)
            .snippet("dubai")
            .icon(BitmapDescriptorFactory.fromBitmap(mapbitmap)));
Run codeHide result

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;
    }
Run codeHide result
+4
source share
1 answer

The answer is in the question itself.

Textures with dimensions4096x8192 are larger than the maximum supported size 4096x4096

Therefore, you must reduce the size of the image below 4096x4096 using any image editing software such as Photoshop.

if you are not familiar, here is a tutorial .

-1

Source: https://habr.com/ru/post/1623550/


All Articles