Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(f[which].equals("Camera")) {
- try {
- String imageFileName="outputImage_" + System.currentTimeMillis()+".png";
- File outputImageFile=new File(pictureSaveFolderPath,imageFileName);
- if(outputImageFile.exists()){
- outputImageFile.delete();
- }
- outputImageFile.createNewFile();
- imageUri=getImageUriByOsVersion(outputImageFile);
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
- startActivityForResult(intent, 1);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- if (requestCode == 1) {
- Uri selectedImage=imageUri;
- getApplicationContext().getContentResolver().notifyChange(selectedImage,null);
- ContentResolver cr=getApplicationContext().getContentResolver();
- try {
- thumbnail = MediaStore.Images.Media.getBitmap(
- cr, selectedImage);
- imageView.setImageBitmap(thumbnail);
- common.setBitmap(thumbnail);
- }catch (Exception e){
- e.printStackTrace();
- }
- Uri tempUri = getImageUri(getApplicationContext(), thumbnail);
- File finalFile = new File(getRealPathFromURI(tempUri));
- }
- private boolean checkAndRequestPermissions() {
- int camera = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
- int writePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
- int readPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
- List<String> listPermissionsNeeded = new ArrayList<String>();
- if (camera != PackageManager.PERMISSION_GRANTED) {
- listPermissionsNeeded.add(Manifest.permission.CAMERA);
- }
- if (writePermission != PackageManager.PERMISSION_GRANTED){
- listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
- }if (readPermission != PackageManager.PERMISSION_GRANTED){
- listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
- }
- if (!listPermissionsNeeded.isEmpty()) {
- ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
- return false;
- }
- return true;
- }
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case REQUEST_ID_MULTIPLE_PERMISSIONS: {
- Map<String, Integer> perms = new HashMap<>();
- perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
- perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
- perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
- if (grantResults.length > 0) {
- for (int i = 0; i < permissions.length; i++)
- perms.put(permissions[i], grantResults[i]);
- if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
- } else {
- if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
- showDialogOK("Sorry, Camera and Storage Permission required for this activity. So please ensure the Camera and Storage permissions are enabled in settings",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- switch (which) {
- case DialogInterface.BUTTON_POSITIVE:
- checkAndRequestPermissions();
- break;
- } }
- });
- }
- } }
- } }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement