Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CameraActivity extends AppCompatActivity
- {
- private static final int PERMISSION_REQUEST_CODE = 1;
- ImageView pic;
- Button capture;
- Uri file;
- @Override
- protected void onCreate(Bundle b)
- {
- super.onCreate(b);
- setContentView(R.layout.cameralayout);
- capture = findViewById(R.id.cameratpicture);
- pic = findViewById(R.id.picture);
- if (Build.VERSION.SDK_INT >= 23)
- {
- if (checkPermission())
- {
- Log.e("permission", "Permission already granted.");
- }
- else
- {
- requestPermission();
- }
- }
- capture.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (checkPermission()){
- takePicture();
- }
- else{
- Toast.makeText(getApplicationContext(),"Permission denied",Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- public void takePicture() {
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- file = Uri.fromFile(getOutputMediaFile());
- intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
- startActivityForResult(intent, 100);
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == 100) {
- if (resultCode == RESULT_OK) {
- pic.setImageURI(file);
- }
- }
- }
- private static File getOutputMediaFile(){
- File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_PICTURES), "CameraDemo");
- if (!mediaStorageDir.exists()){
- if (!mediaStorageDir.mkdirs()){
- return null;
- }
- }
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- return new File(mediaStorageDir.getPath() + File.separator +
- "IMG_"+ timeStamp + ".jpg");
- }
- private boolean checkPermission()
- {
- int camerapermission = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA);
- int storagepermission = ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE);
- if(camerapermission == PackageManager.PERMISSION_GRANTED && storagepermission == PackageManager.PERMISSION_GRANTED)
- {
- return true;
- }
- else {
- return false;
- }
- }
- private void requestPermission()
- {
- ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE);
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[]grantResults)
- {
- switch(requestCode) {
- case PERMISSION_REQUEST_CODE:
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(getApplicationContext(), "Permission accepted", Toast.LENGTH_SHORT).show();
- }
- else {
- if (grantResults[0] == PackageManager.PERMISSION_DENIED || grantResults[1] == PackageManager.PERMISSION_DENIED) {
- Toast.makeText(getApplicationContext(), "Camera or Storage Permission denied", Toast.LENGTH_SHORT).show();
- }
- capture.setEnabled(false);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement