Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. public static int calculateInSampleSize(
  2. BitmapFactory.Options options, int reqWidth, int reqHeight) {
  3. // Raw height and width of image
  4. final int height = options.outHeight;
  5. final int width = options.outWidth;
  6. int inSampleSize = 2;
  7.  
  8. if (height > reqHeight || width > reqWidth) {
  9.  
  10. final int halfHeight = height / 2;
  11. final int halfWidth = width / 2;
  12.  
  13. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  14. // height and width larger than the requested height and width.
  15. while ((halfHeight / inSampleSize) >= reqHeight
  16. && (halfWidth / inSampleSize) >= reqWidth) {
  17. inSampleSize *= 2;
  18. }
  19. }
  20.  
  21. return inSampleSize;
  22. }
  23.  
  24. public static Bitmap decodeSampledBitmapFromResource(int reqWidth, int reqHeight,byte[] bytes) {
  25.  
  26. // First decode with inJustDecodeBounds=true to check dimensions
  27. final BitmapFactory.Options options = new BitmapFactory.Options();
  28. options.inJustDecodeBounds = true;
  29. options.inInputShareable=true;
  30. options.inPurgeable=true;
  31. // BitmapFactory.decodeResource(res, resId, options);
  32. BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
  33. // Calculate inSampleSize
  34. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  35.  
  36. // Decode bitmap with inSampleSize set
  37. options.inJustDecodeBounds = false;
  38. return BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
  39. }
  40.  
  41. BitmapFactory.Options options = new BitmapFactory.Options();
  42. BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length,options);
  43. options.inJustDecodeBounds = true;
  44. options.inInputShareable=true;
  45. options.inPurgeable=true;
  46. int imageHeight = options.outHeight;
  47. int imageWidth = options.outWidth;
  48. String imageType = options.outMimeType;
  49. Drawable d=new BitmapDrawable(getResources(),decodeSampledBitmap(50,50));
  50. Glide.with(getActivity())
  51. .load(d).into(offerBinding.restImg);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement