Guest User

Untitled

a guest
Nov 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. 1. Click a button to take a photo.
  2. 2. Save photo.
  3. 3. Add photo into the gallery.
  4. 4. Show photo into an ImageView.
  5.  
  6. photoFile = createImageFile();
  7.  
  8. private File createImageFile() throws IOException {
  9. // Create an image file name
  10. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  11. String imageFileName = timeStamp;
  12. File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageFileName + ".jpg");
  13.  
  14. if (imageFile.exists()){
  15. imageFile.delete();
  16. imageFile.createNewFile();
  17. }
  18. else{
  19. imageFile.createNewFile();
  20. }
  21.  
  22. mCurrentPhotoPath = imageFile.getAbsolutePath();
  23.  
  24. return imageFile;
  25. }
  26.  
  27. if (photoFile != null) {
  28. Uri photoURI = FileProvider.getUriForFile(getContext(),
  29. "es.antonio.prueba.fileprovider",
  30. photoFile);
  31. takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
  32. startActivityForResult(takePhotoIntent, REQUEST_CAMERA);
  33. }
  34.  
  35. private void galleryAddPic() {
  36. Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  37. File f = new File(mCurrentPhotoPath);
  38. Uri contentUri = Uri.fromFile(f);
  39. mediaScanIntent.setData(contentUri);
  40. getContext().sendBroadcast(mediaScanIntent);
  41. }
  42.  
  43. private void setPic(ImageView mImageView) {
  44. BitmapFactory.Options bmOptions = new BitmapFactory.Options();
  45.  
  46. // Decode the image file into a Bitmap sized to fill the View
  47. bmOptions.inJustDecodeBounds = false;
  48. bmOptions.inPurgeable = true;
  49.  
  50. Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  51. mImageView.setImageBitmap(bitmap);
  52. }
Add Comment
Please, Sign In to add comment