Load Image into map Marker using android - android

I am trying to add bitmap to google map marker icon.
Below is my code to add image to marker icon. Application is crashing because of bitmap size
Error : java.lang.IllegalArgumentException: Textures with dimensions4096x8192 are larger than the maximum supported size 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)));
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;
}

Answer is in your question itself.
Textures with dimensions4096x8192 are larger than the maximum supported size 4096x4096
So you must reduce the image size below 4096x4096 using any image editors such as Photoshop.
if you are not familiar , Here is a tutorial.

Related

How to reduce bitmap quality without affecting it's size

is there a way i can compress an image of 115kb to be 4kb in android Without affecting it size?. just reducing it quality?
i only know of using
BitmapFactory.Options which reduces both size and quality
Bitmap.compress which does not give you options for specifying size in bytes.
public Bitmap compressImage(String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(new FileInputStream(imagePath),null, options);
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(new FileInputStream(imagePath),null, options);
return bmp;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}
Image re sizing means you're going to shorten the resolution of the image. suppose user selects a 1000*1000 px image. you're going to convert the image into a 300*300 image. thus image size will be reduced.
And Image compression is lowering the file size of the image without compromising the resolution. Of course lowering file size will affect the quality of the image. There are many compression algorithm available which can reduce the file size without much affecting the image quality.
One handy way I found in here is :
Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
Log.e("Original dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());
gives
12-07 17:43:36.333: E/Original dimensions(278): 1024 768 12-07
17:43:36.333: E/Compressed dimensions(278): 1024 768
public static int getSquareCropDimensionForBitmap(Bitmap bitmap)
{
int dimension;
//If the bitmap is wider than it is tall
//use the height as the square crop dimension
if (bitmap.getWidth() >= bitmap.getHeight())
{
dimension = bitmap.getHeight();
}
//If the bitmap is taller than it is wide
//use the width as the square crop dimension
else
{
dimension = bitmap.getWidth();
}
return dimension;
}
int dimension = getSquareCropDimensionForBitmap(bitmap);
System.out.println("before cropped height " + bitmap.getHeight() + "and width: " + bitmap.getWidth());
Bitmap croppedBitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
System.out.println("after cropped height "+croppedBitmap.getHeight() +"and width: " + croppedBitmap.getWidth());
it can crop and can reduce size u can specify ur own size

decoding bitmap efficiently, avoiding OOM exceptions

I've written a multimedia app. In this I have made a gallery with a viewpager, and a FragmentStatePagerAdapter. I used FragmentStatePagerAdapter so that each time I'have in memory only two images and each time the user scrolls the pager the old picture is destroyed and the new is decoded in bitmap.
I have read lots of tutorials of how to efficiently load a bitmap using in a smart way the inSampleSize option and I've implemented it in such a way. In my phone runs great, but in some phones I get an out of memory exception so this is how I decode my ImageView in each fragment
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
ImageArray=org.apache.commons.io.FileUtils.readFileToByteArray(new File(_imagePaths.get(mImageNum)));
if(MyApplication.getDeviceWidth() !=0 || MyApplication.getDeviceHeight()!=0)
bitmap=decodeSampledBitmapFromResource(ImageArray,MyApplication.getDeviceWidth(), MyApplication.getDeviceHeight());
else{
DisplayMetrics metrics = MyApplication.getAppContext().getResources().getDisplayMetrics();
bitmap=decodeSampledBitmapFromResource(ImageArray,metrics.widthPixels, metrics.heightPixels);
}
mImageView.setImageBitmap(bitmap);
} catch (IOException e) {
Log.e("ImageDetailFragment",e.getLocalizedMessage());
}
}
public static Bitmap decodeSampledBitmapFromResource(byte[] array,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap b =BitmapFactory.decodeByteArray(array, 0, array.length,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
if(b!=null)
b.recycle();
Log.i("inSampleSize", String.valueOf(options.inSampleSize));
return BitmapFactory.decodeByteArray(array, 0, array.length,options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth,int reqHeight) {
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;
}
Log.i("inSample size", String.valueOf(inSampleSize));
if(sampleLimit==-1)
return 2;
return inSampleSize;
}
So as you can see (I think) I load each bitmap efficiently and if a screen is smaller inSampleSize goes to 2
My question is how can I compute inSampleSize not depending on screen size only but also on the memory that is available to my app, cause in my phone I get 64MB but i've come across tablets with 38MB or less

Scaling Icon to fit in Imageview Android

I have a list in my application that contains all the installed applications with their icons, I'm able to render the installed applications and the icons as well but it is consuming lot of memory as it loads the drawables(icons) of the installed applications into memory as well.
I want to scale down the icon and then load into memory just to reduce the memory usage of the application. Can anyone tell me how that can be achieved.
Note : if PNG format then it will not compress your image because PNG is a lossless format.
and applications icons are in PNG format
Any way to reduce the memory allocation for the icons??
Yeah it's all in the docs:
http://developer.android.com/training/displaying-bitmaps/index.html
Calculate your sample size, i.e. size of the bitmap you want:
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;
}
The decode your bitmap at this size:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
This should all be done off the UI thread:
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
You can then cache the bitmaps so you don't have to do it more times than necessary:
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Use inSampleSize from BitmapFactory.Options
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //Downsample 10x

Is there difference between .jpg and .JPG for android?

I wonder if there is any difference between image extensions .jpg and .JPG. I encountered a problem in that .JPG images cannot be opened with the following code:
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
Log.e("PATH", path);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// Calculate inSampleSizeŠ°
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeFile(path, options);
if(b!=null){
Log.d("bitmap","decoded sucsessfully");
}else{
Log.d("bitmap","decoding failed; options: "+options.toString());
}
return b;
}
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;
Log.d("bitmap", "original options: height "+ height + " widnth "+width+" inSampleSize "+inSampleSize);
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;
Log.d("bitmap", "options: height "+ height + " widnth "+width+" inSampleSize "+inSampleSize);
}
return inSampleSize;
}
If I call
decodeSampledBitmapFromResource(path, 80, 60)
on *.JPG image, the returned bitmap is null.
I'm asking if they are generally implemented in different way or it is specific for Galaxy S3? (I was not able to repeat it on Galaxy Tab 7.7 or on HTC phones.)
Android runs on Linux operating system, which is case sensitive.
If the file has the extension in lowercase, like this: ".jpg", so you should refer in the source code. If the file has the extension ".JPG", also, the same should be written in code.

Can not understand this scale bitmap code

In one of the tutorials Google proposes the following scaling bitmap algorithm to make a bitmap fit a given window:
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) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
( http://developer.android.com/training/displaying-bitmaps/load-bitmap.html )
I don't understand the if(width > height) part
Suppose a bitmap has its height of 5 and its width of 2. Then suppose a window has the height of 1 and the width of 1. It looks like the bitmap won't fit the window then.
It makes sure that the size ratio is respected

Categories

Resources