Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1.  
  2. public class CameraActivity extends AppCompatActivity
  3. {
  4. private static final int PERMISSION_REQUEST_CODE = 1;
  5. ImageView pic;
  6. Button capture;
  7. Uri file;
  8.  
  9. @Override
  10. protected void onCreate(Bundle b)
  11. {
  12. super.onCreate(b);
  13. setContentView(R.layout.cameralayout);
  14. capture = findViewById(R.id.cameratpicture);
  15. pic = findViewById(R.id.picture);
  16. if (Build.VERSION.SDK_INT >= 23)
  17. {
  18. if (checkPermission())
  19. {
  20. Log.e("permission", "Permission already granted.");
  21. }
  22. else
  23. {
  24. requestPermission();
  25. }
  26. }
  27. capture.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. if (checkPermission()){
  31. takePicture();
  32. }
  33. else{
  34. Toast.makeText(getApplicationContext(),"Permission denied",Toast.LENGTH_SHORT).show();
  35. }
  36.  
  37. }
  38. });
  39. }
  40. public void takePicture() {
  41. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  42. file = Uri.fromFile(getOutputMediaFile());
  43. intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
  44. startActivityForResult(intent, 100);
  45. }
  46.  
  47. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  48. if (requestCode == 100) {
  49. if (resultCode == RESULT_OK) {
  50. pic.setImageURI(file);
  51. }
  52. }
  53. }
  54.  
  55. private static File getOutputMediaFile(){
  56. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  57. Environment.DIRECTORY_PICTURES), "CameraDemo");
  58.  
  59. if (!mediaStorageDir.exists()){
  60. if (!mediaStorageDir.mkdirs()){
  61. return null;
  62. }
  63. }
  64.  
  65. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  66. return new File(mediaStorageDir.getPath() + File.separator +
  67. "IMG_"+ timeStamp + ".jpg");
  68. }
  69. private boolean checkPermission()
  70. {
  71. int camerapermission = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA);
  72. int storagepermission = ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE);
  73. if(camerapermission == PackageManager.PERMISSION_GRANTED && storagepermission == PackageManager.PERMISSION_GRANTED)
  74. {
  75. return true;
  76. }
  77. else {
  78. return false;
  79. }
  80. }
  81. private void requestPermission()
  82. {
  83. ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE);
  84. }
  85. @Override
  86. public void onRequestPermissionsResult(int requestCode, String permissions[], int[]grantResults)
  87. {
  88. switch(requestCode) {
  89. case PERMISSION_REQUEST_CODE:
  90. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
  91. Toast.makeText(getApplicationContext(), "Permission accepted", Toast.LENGTH_SHORT).show();
  92. }
  93. else {
  94. if (grantResults[0] == PackageManager.PERMISSION_DENIED || grantResults[1] == PackageManager.PERMISSION_DENIED) {
  95. Toast.makeText(getApplicationContext(), "Camera or Storage Permission denied", Toast.LENGTH_SHORT).show();
  96. }
  97. capture.setEnabled(false);
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement