Reputation: 89
I am trying to use the camera and for some reason that I cannot yet find, the camera stop working days ago
I have the maniset permissions and now when I try to take a picture it says
"java.io.ioexception permission denied"
This is the manifest
<uses-permission android:name="android.permission.CAMERA" />
and this is how I take the picture
try {
mPhotoFile = Tools.createImageFile();
new CameraService().takePicture(CameraService.REQUEST_IMAGE_CAPTURE, contextActivity, mPhotoFile);
} catch (IOException ex) {
Log.e(TAG, "btnTakePictureSale error "+ex);
ex.printStackTrace();
}
Any help or suggestion on what to look for woudl be great. Again, it was working fine last week
I use the app over serveral versions of android and fine the same issue
Upvotes: 1
Views: 7848
Reputation: 843
try putting
android:requestLegacyExternalStorage="true"
on your manifest inside the application tag. maybe the camera permission is ok but the external write is not
Upvotes: 2
Reputation: 35
use this for request runtime permission from user:
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
{
try {
mPhotoFile = Tools.createImageFile();
new CameraService().takePicture(CameraService.REQUEST_IMAGE_CAPTURE, contextActivity, mPhotoFile);
} catch (IOException ex) {
Log.e(TAG, "btnTakePictureSale error "+ex);
ex.printStackTrace();
}
}else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 101);
}
Upvotes: 0
Reputation: 1706
If you added this line inside your manifest file:
Then you also need to ask for that permission once you run your app. To do this you need to set a variable in your class above onCreate like this:
private static final int MY_CAMERA_REQUEST_CODE = 100;
After that, in your onCreate copy this:
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_REQUEST_CODE);
}
This will check if your user approved for you to use his phone camera. If not, it will ask him to do so, the alert dialog will be shown. After his reply, you need to get that reply and do accordingly. To do that add this as function:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
Inside this line:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
}
you can do what you want, ex. start a new activity or else. Every time you want to start the camera, just check for the permission as above to see if the user approved it, if not notify him or ask him again like this in your other activity.
To start Camera to take a photo you can use this:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
} catch (ActivityNotFoundException e) {
// display error state to the user
}
}
The following code retrieves this image and displays it in an ImageView
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
And to save your photos on the device you need these two permissions also:
<manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
Remember this, there are two different types of permissions on Android.
All three of permissions you need to use are categorized as Dangerous:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
So you'll have to ask the user for that permission.
Check this documentation on Take photos for more info. Check this documentation on Request app permissions
Upvotes: 0