Reputation: 189
I'm trying to make a basic camera application that can access the saved photo from the gallery (needed as part of another app but due to problems I've been having I am developing this in a blank project), and have been following mainly this tutorial https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media
Having then realised that it would just crash because of how permissions work in Marshmallow, and needing backward compatibility, I've tried to follow tutorials on implementing permission requests so that I can actually use the app.
This is what I currently have after several hours of trying. I have added permissions in the manifest, but as these are fairly standard I have not bothered to copy and paste these over. This currently crashes on the test() method because of there not being a group called Storage. With that line commented out, it will just say permission denied without prompting me to sort permissions (whether on a Marshmallow device or not). Frankly I am now at a loss. What I need this to do is before launching the camera in the onLaunchCamera method (which is launched off a button click), to get the permissions for reading and writing external storage and for accessing the camera. Any help you are able to give would be much appreciated.
private boolean cameraPermissionsCheck() {
return ContextCompat.checkSelfPermission(this, Manifest.permission_group.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
private boolean storagePermissionsCheck() {
return ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.CAMERA, Manifest.permission_group.STORAGE}, 123);
}
private void test() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission_group.STORAGE)) {
//was a toast notification here
requestPermissions();
} else {
requestPermissions();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123
&& grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
public void onLaunchCamera(View view) {
//btn = (Button) findViewById(R.id.button);
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
if(!cameraPermissionsCheck() || !storagePermissionsCheck()){
test();
}
else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
} else {
Toast.makeText(MainActivity.this, "No Camera",
Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Views: 283
Reputation: 1388
Try this
public void onLaunchCamera(View view) {
//btn = (Button) findViewById(R.id.button);
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
checkPermission();
}
else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
} else {
Toast.makeText(MainActivity.this, "No Camera",
Toast.LENGTH_LONG).show();
}
}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
123);
} else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,getPhotoFileUri(photoFileName)); // set the image file name
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, 0);
}
}
And make sure you have set proper version in your build.gradle
**compileSdkVersion 23
buildToolsVersion "23.0.2"**
defaultConfig {
applicationId "your_package_name"
minSdkVersion 15
**targetSdkVersion 23**
versionCode 1
versionName "1.0"
multiDexEnabled true
}
Upvotes: 1