0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kalaivani R
  • 327
  • 1
  • 4
  • 11
  • https://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap – Quick learner Apr 06 '18 at 07:42
  • https://stackoverflow.com/questions/20441644/java-lang-outofmemoryerror-bitmapfactory-decodestrpath – Quick learner Apr 06 '18 at 07:43
  • `I referred this link` there are no links in the question. – Vladyslav Matviienko Apr 06 '18 at 07:43
  • android:largeHeap="true" add in application tag in manifest – Quick learner Apr 06 '18 at 07:43
  • https://stackoverflow.com/questions/25719620/how-to-solve-java-lang-outofmemoryerror-trouble-in-android – Quick learner Apr 06 '18 at 07:43
  • @quicklearner, it is a workaround, not a solution. He is trying to load 53 MB bitmaps. Large heap can work until he gets as large image as it will be large even for large heap. – Vladyslav Matviienko Apr 06 '18 at 07:45
  • https://developer.android.com/topic/performance/graphics/load-bitmap.html – Vladyslav Matviienko Apr 06 '18 at 07:46
  • 1
    `android:largeHeap="true"` never a solution .With this You are just manipulating the chances of error. See [Loading Large Bitmaps Efficiently](https://developer.android.com/topic/performance/graphics/load-bitmap.html). Use small size compressed images. – ADM Apr 06 '18 at 07:47
  • Possible duplicate of [How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size)](https://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap) – Cleptus Apr 06 '18 at 07:48
  • i added android:largeHeap="true" eventhough it will happen.i referred the above link BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; mBitmapInsurance = BitmapFactory.decodeFile(mCurrentPhotoPath,options);,but in decodefile i cant able to pass the imageurl – Kalaivani R Apr 06 '18 at 08:00
  • anyone give proper answer please – Kalaivani R Apr 06 '18 at 08:00

0 Answers0