Guest User

Untitled

a guest
Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. Options opts = new BitmapFactory.Options();
  2. BufferedInputStream bis = new BufferedInputStream(pis);
  3. bis.mark(1024 * 1024);
  4. opts.inJustDecodeBounds = true;
  5. Bitmap bmImg=BitmapFactory.decodeStream(bis,null,opts);
  6.  
  7. Log.e("optwidth",opts.outWidth+"");
  8. try {
  9. bis.reset();
  10. opts.inJustDecodeBounds = false;
  11. int ratio = opts.outWidth/800;
  12. Log.e("ratio",String.valueOf(ratio));
  13. if (opts.outWidth>=800)opts.inSampleSize = ratio;
  14.  
  15. return BitmapFactory.decodeStream(bis,null,opts);
  16.  
  17. } catch (IOException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. return null;
  21. }
  22.  
  23. File photos= new File("imageFilePath that you select");
  24. Bitmap b = decodeFile(photos);
  25.  
  26. private Bitmap decodeFile(File f){
  27. try {
  28. //decode image size
  29. BitmapFactory.Options o = new BitmapFactory.Options();
  30. o.inJustDecodeBounds = true;
  31. BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  32.  
  33. //Find the correct scale value. It should be the power of 2.
  34. final int REQUIRED_SIZE=70;
  35. int width_tmp=o.outWidth, height_tmp=o.outHeight;
  36. int scale=1;
  37. while(true){
  38. if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
  39. break;
  40. width_tmp/=2;
  41. height_tmp/=2;
  42. scale++;
  43. }
  44.  
  45. //decode with inSampleSize
  46. BitmapFactory.Options o2 = new BitmapFactory.Options();
  47. o2.inSampleSize=scale;
  48. return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  49. } catch (FileNotFoundException e) {}
  50. return null;
  51. }
  52.  
  53. ImageView img = (ImageView)findViewById(R.id.sdcardimage);
  54. img.setImageBitmap(b);
Add Comment
Please, Sign In to add comment