selvalives

Untitled

Dec 11th, 2019
113
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.nus.app1;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7.  
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.os.Environment;
  11. import android.os.StrictMode;
  12. import android.provider.MediaStore;
  13. import android.view.View;
  14. import android.widget.Button;
  15. import android.widget.ImageView;
  16. import android.widget.Toast;
  17.  
  18. import androidx.core.app.ActivityCompat;
  19. import androidx.core.content.ContextCompat;
  20.  
  21. import java.io.File;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24.  
  25. public class CameraActivity extends Activity
  26. {
  27. private static final int PERMISSION_REQUEST_CODE = 1;
  28. ImageView iv;
  29. Button btncapture;
  30. private Uri file;
  31.  
  32. public void onCreate(Bundle b)
  33. {
  34. StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
  35. StrictMode.setVmPolicy(builder.build());
  36. super.onCreate(b);
  37. setContentView(R.layout.cameralayout);
  38. iv=findViewById(R.id.iv);
  39. btncapture=findViewById(R.id.btncapture);
  40. btncapture.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. if(checkPermission())
  44. {
  45. takePicture();
  46. }
  47. else
  48. {
  49. requestPermission();
  50. }
  51. }
  52. });
  53. }
  54. private boolean checkPermission()
  55. {
  56. int camerapermission = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA);
  57. int storagepermission = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
  58. if (camerapermission == PackageManager.PERMISSION_GRANTED && storagepermission ==PackageManager.PERMISSION_GRANTED)
  59. {
  60. return true;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. private void requestPermission()
  68. {
  69. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
  70. }
  71. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
  72. {
  73. switch (requestCode)
  74. {
  75. case PERMISSION_REQUEST_CODE:
  76. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)
  77. {
  78. Toast.makeText(getApplicationContext(),"Permission accepted", Toast.LENGTH_LONG).show();
  79. takePicture();
  80. }
  81. else
  82. {
  83. Toast.makeText(getApplicationContext(),"Permission denied", Toast.LENGTH_LONG).show();
  84. }
  85. }
  86. }
  87.  
  88. public void takePicture() {
  89. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  90. file = Uri.fromFile(getOutputMediaFile());
  91. intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
  92.  
  93. startActivityForResult(intent, 100);
  94. }
  95. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  96. if (requestCode == 100) {
  97. if (resultCode == RESULT_OK) {
  98. iv.setImageURI(file);
  99. }
  100. }
  101. }
  102. private static File getOutputMediaFile(){
  103. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  104. Environment.DIRECTORY_PICTURES), "CameraDemo");
  105.  
  106. if (!mediaStorageDir.exists()){
  107. if (!mediaStorageDir.mkdirs()){
  108. return null;
  109. }
  110. }
  111.  
  112. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  113. return new File(mediaStorageDir.getPath() + File.separator +
  114. "IMG_"+ timeStamp + ".jpg");
  115. }
  116. }
RAW Paste Data