Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public void loadImagefromGallery(View view) {
  2. // Create intent to Open Image applications like Gallery, Google Photos
  3. Intent galleryIntent = new Intent(Intent.ACTION_PICK,
  4. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  5.  
  6. galleryIntent.setType("image/*");
  7. galleryIntent.putExtra("crop", "true");
  8. galleryIntent.putExtra("outputX", 200);
  9. galleryIntent.putExtra("outputY", 200);
  10. galleryIntent.putExtra("aspectX", 1);
  11. galleryIntent.putExtra("aspectY", 1);
  12. galleryIntent.putExtra("scale", true);
  13.  
  14. galleryIntent.putExtra(MediaStore.EXTRA_OUTPUT,UriwhereToStore);
  15. galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  16.  
  17. // Start the Intent
  18. startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
  19. }
  20.  
  21.  
  22.  
  23. @Override
  24. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  25. super.onActivityResult(requestCode, resultCode, data);
  26. try {
  27. // When an Image is picked
  28. if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
  29. && null != data) {
  30. // Get the Image from data
  31.  
  32. Uri selectedImage = data.getData();
  33. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  34.  
  35. // Get the cursor
  36. Cursor cursor = getContentResolver().query(selectedImage,
  37. filePathColumn, null, null, null);
  38. // Move to first row
  39. cursor.moveToFirst();
  40.  
  41. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  42. imgDecodableString = cursor.getString(columnIndex);
  43. cursor.close();
  44. ImageView imgView = (ImageView) findViewById(R.id.imgView);
  45. // Set the Image in ImageView after decoding the String
  46. imgView.setImageBitmap(BitmapFactory
  47. .decodeFile(imgDecodableString));
  48.  
  49. } else {
  50. Toast.makeText(this, "You haven't picked Image",
  51. Toast.LENGTH_LONG).show();
  52. }
  53. } catch (Exception e) {
  54. Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
  55. .show();
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement