Advertisement
selvalives

Untitled

Dec 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. android.permission.CAMERA
  2. android.permission.WRITE_EXTERNAL_STORAGE
  3.  
  4.  
  5. public void takePicture() {
  6. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  7. file = Uri.fromFile(getOutputMediaFile());
  8. intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
  9.  
  10. startActivityForResult(intent, 100);
  11. }
  12.  
  13. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  14. if (requestCode == 100) {
  15. if (resultCode == RESULT_OK) {
  16. imageView.setImageURI(file);
  17. }
  18. }
  19. }
  20.  
  21. private static File getOutputMediaFile(){
  22. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  23. Environment.DIRECTORY_PICTURES), "CameraDemo");
  24.  
  25. if (!mediaStorageDir.exists()){
  26. if (!mediaStorageDir.mkdirs()){
  27. return null;
  28. }
  29. }
  30.  
  31. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  32. return new File(mediaStorageDir.getPath() + File.separator +
  33. "IMG_"+ timeStamp + ".jpg");
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement