2

I am making an application to capture image once and then save it in SD card and display it on the application's MainActivity's ImageView(image_view)(which is handled in onRequestPermissionsResult() method). The application also helps to send email the picture clicked by user. Now for this I have requested 2 permssions:

1.) Camera Request

2.) External Storage Request

When the app launches the Camera Request dialog box appears but the External Storage Request Dialog box is not appearing and the app crashes displaying the toast as External write permissions has not been granted. Cannot Save File.

I have mentioned the requests in Manifest also

<uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" /> 

Code for requesting permissions

public void requestPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_EXTERNAL_STORAGE);
    }

    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        launchCamera();
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.CAMERA)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.CAMERA},
                REQUEST_CAMERA);
    }
}

Here is the Full Code

public class MainActivity extends AppCompatActivity {

Button clickPhotoButton;
private static final String FILE_NAME="image01.jpg";
private static final int CAMERA_PIC_REQUEST = 100;
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
private static final int REQUEST_CAMERA = 2;
File pictureDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Prakhar");
Uri fileUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    clickPhotoButton = (Button) findViewById(R.id.click_photo_button);

    makeFolder();
}

public void makeFolder(){

    if(!pictureDir.exists()) {
        pictureDir.mkdirs();
    }
    Log.e("Prakhar", pictureDir.getAbsolutePath());
}

public void requestPermissions() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_EXTERNAL_STORAGE);
    }

    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        launchCamera();
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.CAMERA)) {
            Toast.makeText(this,
                    "External storage permission required to save images",
                    Toast.LENGTH_SHORT).show();
        }
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.CAMERA},
                REQUEST_CAMERA);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if(requestCode == REQUEST_WRITE_EXTERNAL_STORAGE ) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            launchCamera();
        } else {
            Toast.makeText(this,
                    "External write permission has not been granted, cannot saved images",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    if(requestCode == REQUEST_CAMERA) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            launchCamera();
        } else {
            Toast.makeText(this,
                    "External write permission has not been granted, cannot saved images",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

public void onPhotoClicked(View view) {

    requestPermissions();
}

public void launchCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File image = new File(pictureDir, FILE_NAME);
    fileUri = Uri.fromFile(image);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_PIC_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
        ImageView imageView = (ImageView) findViewById(R.id.image_view);
        File image = new File(pictureDir, FILE_NAME);
        fileUri = Uri.fromFile(image);
        imageView.setImageURI(fileUri);
    }
}
}
  • Look https://stackoverflow.com/questions/34342816/android-6-0-multiple-permissions – NizarETH Jun 10 '17 at 17:38
  • @Euphor08 Ok i'll try this. But I have a code which is similar to my code. In fact the methods to request permissions is identical and that code is showing correct behavior and mine is not. It must be a small thing that is missing in y code. – Prakhar Singh Jun 10 '17 at 17:43
  • Does the activity you are calling this from extend AppCompatActivity and implement ActivityCompat.OnRequestPermissionsResultCallback? If not, android code just ignores the permission request. – tim.paetz Jun 10 '17 at 18:01
  • If it is ignoring the permission request then why is it doing so just for the external write permission and not for the camera permission. – Prakhar Singh Jun 10 '17 at 18:07
  • @tim.paetz However I did what you said and still the same problem. I have the correct version of the code with me which is similar to my code. I can mail it to you if you want – Prakhar Singh Jun 10 '17 at 18:09
  • You probably need to show more of your code. You may not be using the onRequestPermissionsResult callback in ActivityCompat.OnRequestPermissionsResultCallback correctly. You need to call any code that requires permissions from in that callback and then only on a successful callback. – tim.paetz Jun 10 '17 at 19:18
  • @tim.paetz I have added the full code. Please do have a look. – Prakhar Singh Jun 10 '17 at 19:42
  • Delete the "makeFolder" from onCreate and see if that fixes the problem. If it does, then move that call to after you have received permission to WRITE_EXTERNAL_STORAGE. – tim.paetz Jun 10 '17 at 20:28
  • @tim.paetz No. Still not working. Would you like to have a look at the correct version of a similar code. – Prakhar Singh Jun 11 '17 at 04:35
  • I've run out of ideas. I definitely suggest using Vinayaga's answer though and asking for both permissions at the same time rather than breaking it up. – tim.paetz Jun 11 '17 at 12:33
  • @tim.paetz Found it. It was really a waste of time. In the manifest file I have written `android.permissions.WRITE_EXTERNAL_STORAGE` however it should have been `android.permission.WRITE_EXTERNAL_STORAGE` – Prakhar Singh Jun 11 '17 at 18:09

2 Answers2

1

Found it. It was really a waste of time. In the manifest file I have written

android.permissions.WRITE_EXTERNAL_STORAGE

however it should have been

android.permission.WRITE_EXTERNAL_STORAGE
0

Try like this

 public void requestPermissions() {
    boolean hasStoragePermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

    boolean hasCameraPermission = ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;

    ArrayList<String> permissonList = new ArrayList();

    if (!hasCameraPermission) {
        permissonList.add(android.Manifest.permission.CAMERA)
    }

    if (!hasStoragePermission) {
        permissonList.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

    if (permissonList.size() > 0) {
        String [] permissionArray = permissonList.toArray(new String[]{});

        ActivityCompat.requestPermissions(this,permissionArray,
                REQUEST_PERMISSION);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {

    if (requestCode == REQUEST_PERMISSION) {
        boolean allPermissionGranted = true;

        for (int i =0; i < grantResults.length; i++) {
            boolean permissionGranted = grantResults[i] == PackageManager.PERMISSION_GRANTED;

            if (!permisssionGranted) {
                allPermissionGranted = false;
                break;
            }
        }


        if (allPermissionGranted) {
            launchCamera();
        }
    }
}
VinayagaSundar
  • 1,673
  • 17
  • 18