Guest User

Untitled

a guest
Nov 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. public class ObtainImage {
  2.  
  3. private Uri imgUri;
  4.  
  5. /** Here goes your code where you handle the way the user chooses between getting from gallery or opening camera **/
  6.  
  7. /**
  8. * Opens gallery to choose image.
  9. */
  10. private void chooseFromGallery() {
  11. Intent intent = new Intent(
  12. Intent.ACTION_PICK,
  13. MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  14. intent.setType("image/*");
  15. startActivityForResult(
  16. Intent.createChooser(intent, "Select File"),
  17. SELECT_FILE);
  18. }
  19.  
  20. /**
  21. * Creates empty image file. Launches camera app to capture image.
  22. */
  23. private void captureImage() {
  24. File image = createImageFile();
  25. imgUri = Uri.fromFile(image);
  26. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  27. intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
  28. // start the image capture Intent
  29. startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
  30. }
  31.  
  32. /**
  33. * Creates empty image file in external storage
  34. *
  35. * @return File
  36. */
  37. private File createImageFile() {
  38. // External location
  39. //TODO: Ver si cambiar a almacenamiento interno o externo no publico
  40. File mediaStorageDir = new File(
  41. Environment
  42. .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
  43. AppConfig.IMAGE_DIRECTORY_NAME);
  44.  
  45. // Create the storage directory if it does not exist
  46. if (!mediaStorageDir.exists()) {
  47. if (!mediaStorageDir.mkdirs()) {
  48. Log.d(TAG, "Oops! Failed create "
  49. + AppConfig.IMAGE_DIRECTORY_NAME + " directory");
  50. return null;
  51. }
  52. }
  53.  
  54. // Create a media file name
  55. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
  56. Locale.getDefault()).format(new Date());
  57. File mediaFile;
  58. mediaFile = new File(mediaStorageDir.getPath() + File.separator
  59. + "IMG_" + timeStamp + ".jpg");
  60.  
  61.  
  62. return mediaFile;
  63. }
  64.  
  65. /**
  66. * Receiving activity result method will be called after closing the camera or choosing image from gallery
  67. */
  68. @Override
  69. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  70. if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
  71. if (resultCode == Activity.RESULT_OK) {
  72. /** If the image comes from camera **/
  73. }
  74. } else if (requestCode == SELECT_FILE) {
  75. if (resultCode == Activity.RESULT_OK) {
  76. imgUri = data.getData();
  77. /** If the image comes from gallery **/
  78. }
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment