Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. /**
  2. * This function is called when the add player picture button is clicked.
  3. * It accesses the devices gallery and the user can choose a picture
  4. * from the gallery.
  5. * Or if the user chooses to take a picture with the camera, it handles that
  6. */
  7.  
  8. @Override
  9. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  10. super.onActivityResult(requestCode, resultCode, data);
  11. switch (requestCode) {
  12. case TAKE_PICTURE:
  13. if (resultCode == Activity.RESULT_OK) {
  14. Uri selectedImage = imageUri;
  15. getContentResolver().notifyChange(selectedImage, null);
  16. ContentResolver cr = getContentResolver();
  17. this.picPath = selectedImage.getPath();
  18. Bitmap bitmap;
  19. try {
  20. bitmap = android.provider.MediaStore.Images.Media
  21. .getBitmap(cr, selectedImage);
  22. imageView = (ImageView) findViewById(R.id.imagePlayer);
  23. imageView.setImageBitmap(bitmap);
  24. Toast.makeText(this, selectedImage.toString(),
  25. Toast.LENGTH_LONG).show();
  26. } catch (Exception e) {
  27. Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
  28. .show();
  29. Log.e("Camera", e.toString());
  30. }
  31. }
  32.  
  33. Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50
  34.  
  35. // below 3 line of code will come instead of
  36. //imageView.setImageBitmap(bitmap);
  37. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  38. photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
  39. imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));
  40.  
  41. // please define following two methods in your activity
  42. public Bitmap decodeSampledBitmapFromByte(byte[] res,
  43. int reqWidth, int reqHeight) {
  44.  
  45. // First decode with inJustDecodeBounds=true to check dimensions
  46. final BitmapFactory.Options options = new BitmapFactory.Options();
  47. options.inJustDecodeBounds = true;
  48. BitmapFactory.decodeByteArray(res, 0, res.length,options);
  49.  
  50. // Calculate inSampleSize
  51. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  52.  
  53. // Decode bitmap with inSampleSize set
  54. options.inJustDecodeBounds = false;
  55. return BitmapFactory.decodeByteArray(res, 0, res.length,options);
  56. }
  57. public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  58. // Raw height and width of image
  59. final int height = options.outHeight;
  60. final int width = options.outWidth;
  61. int inSampleSize = 1;
  62.  
  63. if (height > reqHeight || width > reqWidth) {
  64. if (width > height) {
  65. inSampleSize = Math.round((float)height / (float)reqHeight);
  66. } else {
  67. inSampleSize = Math.round((float)width / (float)reqWidth);
  68. }
  69. }
  70. return inSampleSize;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement