如何使用URL减少位图图像?

时间:2018-04-06 07:40:36

标签: android bitmap

我在我的应用程序中使用Mapbox所以我在地图中显示自定义标记。我将从API获取图像作为图像URL,如果我直接传递url图像不显示所以我转换为Bitmap图像,有时applciation崩溃了。请帮帮我。

java.lang.OutOfMemoryError:无法分配带有4194208个空闲字节的53934348字节分配和36MB直到OOM

注意:在将url转换为位图时,时间本身的应用程序有时会崩溃。

代码:

//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);
    }

我引用此链接 http://madhusudhanrc.blogspot.in/2012/09/reduce-bitmap-size-using.html ,如果我缩小图片意味着它不是diplaying。请帮我减少位图大小。

0 个答案:

没有答案