I am using Mapbox in my application so I'm displaying customizing marker in the map.I will get image from API as image URL,if I directly pass the url image is not displying so I converted as the Bitmap image,sometimes applciation getting crashed.Please help me.
java.lang.OutOfMemoryError: Failed to allocate a 53934348 byte allocation with 4194208 free bytes and 36MB until OOM
Note:while converting the url into bitmap that time itself application crashed sometimes.
Code:
//converting the url into bitmap
try {
URL url = new URL(“http://192.168.0.109/images/profile/2018-04-03-14-01-022017-11-13-07-12-36Image.jpg”);
Bitmap bitmapimage = BitmapFactory.decodeStream(url.openConnection().getInputStream()); }
catch(IOException e) { System.out.println(e); }
//customizing the marker
MarkerViewOptions markerViewOptions = new MarkerViewOptions()
.position(new LatLng(Double.parseDouble(userDetails.getLatitude()), Double.parseDouble(userDetails.getLongitutde())))
.icon(drawableToIcon(getContext(),bitmapimage, userDetails.getUser_status()));
mMapBoxMap.addMarker(markerViewOptions);
//method to customise the layout
public Icon drawableToIcon(@NonNull Context context, Bitmap image, String status) {
View marker = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
CircleImageView markerImage = (CircleImageView) marker.findViewById(R.id.user_dp);
markerImage.setImageBitmap(image);
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
marker.setLayoutParams(new ViewGroup.LayoutParams(52, ViewGroup.LayoutParams.WRAP_CONTENT));
marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
marker.draw(canvas);
return IconFactory.getInstance(context).fromBitmap(bitmap);
}
I referred this link http://madhusudhanrc.blogspot.in/2012/09/reduce-bitmap-size-using.html ,if I shrink the image means its not diplaying.Please help me to reduce the bitmap size.