I am writing a simple inventory management app that takes a picture of items to be logged. So the expected behaviour is that users hit a button to launch the camera, take a picture and return to the app to enter text information. But I keep getting the following Security Exception error
java.lang.SecurityException: Permission Denial: starting Intent {
act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera }
from ProcessRecord{734dbfd 22169:com.virgo19.tinni.teatracker/u0a58}
(pid=22169, uid=10058) with revoked permission android.permission.CAMERA
I have looked around the web for days and there doesn't seem to be any solution since I am already following the Android Developer instructions. Including the instruction about asking for permission at runtime. Code fragments below,
//Code calling camera intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, RETURN_FROM_CAMERA);
}
//On activity return request fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RETURN_FROM_CAMERA && data != null){
//Check permissions
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
//Permission is okay, so get on with getting image from Camera
Bundle extras = data.getExtras();
tinImage = (Bitmap) extras.get("data");
//set image view
setImage();
} else if (permissionCheck != PackageManager.PERMISSION_GRANTED){
//Permission not granted, ask for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
}
}
Can anyone see why this keeps crashing? Thanks!
You need to request the CAMERA permission before taking the picture via startActivityForResult(). Your current code attempts to request this permission after taking the picture.
Related
I am new to android dev and little knowledge of java.I want to know how to inflate a layout from service using SYSTEM_ALERT_WINDOW
public void checkDrawOverlayPermission() {
/* check if we already have
permission to draw over
other apps */
if(android.os.Build.VERSION.SDK_INT > 23) {
if (!Settings.canDrawOverlays(this))
{
/* if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/* request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
else {
createOnTopView();
}}}}
I used that to get permission↑↑↑
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/* check if received result code
is equal our requested code for draw permission */
if (requestCode == REQUEST_CODE && android.os.Build.VERSION.SDK_INT > 22) {
/* if so check once again if we have permission */
if (Settings.canDrawOverlays(this)) {
createOnTopView();
}
}
}
after I run this on my phone Teckno k7 ,android 6.0
It runs well buh can't run on my friend's phone Techno F2.it crashes on his phone.
Please help me.
I just started using Android Studio to develop an experimental application. My current problem in trying to get one of my navigation bar tabs set to a camera tab so that when pressed, the camera opens, user takes a picture, then the program does something with that picture. However, when I tried to set the tab as a camera, the program immediately crashes after clicking on the tab. I have this portion of the code currently set as my Main for that specific tab.
case R.id.navigation.dashboard:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
return true;
Should I use a button as an alternative?
EDIT: changed title and some details to the question for more clarification
EDIT2: Got the problem fixed, thanks! The problem for me was actually not checking for app permissions manually via the app info even though I had these permission scripts on the XML file already.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
So I forgot to add a permission popup on my program.
Couple of thing are wrong with this:
Why use tabs for camera if you are just going to use an intent for the camera picture.
The intent should look something like this:
Note: imageUri should be set here to be used later
void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
Since starting an activity for result you need to declare a variable TAKE_PICTURE:
private static final int TAKE_PICTURE = 123
Maybe you want to use tab for showing the picture, and that can look like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(getContentResolver(), selectedImage);
// show image here in an imageView
} catch (Exception e) {
Log.e("Camera", e.toString());
}
}
}
}
If you don't want to open new Activity you can use CameraX library.
Here you have nice sample how to use it.
I make an application that can update images to the server. There are 2 options : Take picture from camera and select from library. the code works for Select from library choice and when I click on Take picture the app crash with these report.
How to handle this ?
My code for capture image intent :
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
captureImage();
}
});
private void captureImage() {
Intent intentCap = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentCap.setType("image/*");
startActivityForResult(intentCap, 0);
}
error
Seems like Android cant found suitable Intent for this.
Try this Intent:
try{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
// show message to user
}
You should always check for the resolveActivity when calling intent like this of third party.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Refer this
First and foremost check that you have added the required permissions in your Manifest file and then test your app. Even if the app doesn't run after that then that maybe occurring because of the following reasons:
1) There may not be a camera on your device
2) There is no SD card in your phone
In this case you can refer the following links which describe solutions to the similar problem link 1 and link 2
Most probably it could be possible that you must have forgot to add Runtime Permissions to access Camera API and thus resulting in App Crash when you try to open camera. Below is the code snipped which you can use to do the same:
public void showCamera(View view) {
// BEGIN_INCLUDE(camera_permission)
// Check if the Camera permission is already available.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Camera permission has not been granted.
requestCameraPermission();
} else {
// Camera permissions is already available, show the camera preview.
Log.i(TAG,
"CAMERA permission has already been granted. Displaying camera preview.");
showCameraPreview();
}
// END_INCLUDE(camera_permission)
}
private void requestCameraPermission() {
// BEGIN_INCLUDE(camera_permission_request)
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
} else {
// Camera permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
// END_INCLUDE(camera_permission_request)
}
Here is Google Runtime Permission Model Video to have an better understanding.
Also check official documentation for further details.
Hope this helps.
I am trying to open the android default video camera from my app, using the following code:
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);
But on my two phones (Samsung Note 2, and Google Pixel), it opens the image camera instead. I have this permission in my manifest:
<uses-permission android:name="android.permission.CAMERA"/>
Any ideas what causes this issue?
I've also requested the permission at runtime.
Add follow permission CAPTURE_SECURE_VIDEO_OUTPUT and CAPTURE_VIDEO_OUTPUT
Android 6.0 and later requieres to ask permissions at run time. Read the official doc here: https://developer.android.com/training/permissions/requesting.html
I hope it helps.
You must add next code. Devices have Android 6.0 or later.
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
}
If your the app has the permission for use the STORAGE we open the STORAGE.
If your the app doesn't have permission for use STORAGE, we open system-dialog.
Result from the dialog you can see in the onRequestPermissionsResult. (You must override it on your Activity).
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == 2) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);
}
}
}
I think you must extract next lines to the private method.
private void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, 1);
}
For more information see : https://developer.android.com/training/permissions/requesting.html
I am having an issue with taking pictures on my Sony Xperia L phone (but it works for other phones like nexus and htc) when it runs as an android activity...
After taking the picture it directly returns back with a result code of RESULT_CANCELED.
following is my code for the application...
Toast.makeText(this, "Taking snapshot", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null){
Toast.makeText(this, " camera ready", Toast.LENGTH_SHORT).show();
startActivityForResult(intent, REQUEST_CAPTURE_PIC);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "back from camera...", Toast.LENGTH_SHORT).show();
if (requestCode == REQUEST_CAPTURE_PIC || resultCode == RESULT_OK) {
Toast.makeText(this, "now saving..", Toast.LENGTH_SHORT).show();
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
img.setImageBitmap(imageBitmap);
}
}
I dont that this is a code issue. I think this has something to do with my phone camera application.
Thanks in advance.