Handle Intent Camera - java

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.

Related

No Activity found to handle Intent when trying to programmatically ask for permissions

I need to get the MANAGE_ALL_FILES_ACCESS_PERMISSION to be able to download and install APK's from my application.
When I was targeting SDK 27 and lower, everything worked fine, when I declared
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the manifest and asked for permission with ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
But now I need to target SDK 30, meaning that WRITE_EXTERNAL_STORAGE is deprecated and not working for API versions 29 and 30.
I have been trying to find a workaround and one that I could think of was starting the Intent to allow users to switch the permission on by themselves but I cannot get it to work.
The code I am using:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
startActivity(intent);
} catch (Exception ex){
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
}
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
}
And the error that I get when trying to open intent:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.MANAGE_ALL_FILES_ACCESS_PERMISSION }
The error comes from the catch block but the Exception ex gives the same error.
What am I doing wrong or is this just not allowed on the current phone I am using (XCover 4s Android 10).
The answer to getting the storage permission that worked for me.
I had to change the Manifest.permission. strings so instead of what was written in the question I used.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.requestPermissions(
activity,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.MANAGE_EXTERNAL_STORAGE
},
1
);
} else {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
}
Or similarly this could be used :
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
Then to get the prompt to allow user to install Apps from unkown sources :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!activity.getPackageManager().canRequestPackageInstalls()) {
activity.startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", activity.getPackageName()))), 1234);
} else {
}
}
I had this problem when I was trying to navigate the user to enable all files access permission on the app screen directly.
Solution for this one is to add the package name like this:
val storageIntent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
Uri.parse("package:$packageName"))

How to get a permission request in new ActivityResult API (1.3.0-alpha05)?

so, I tried to get a permission with the new registerForActivityResult() method and ask for with button click with .launch() and it doesn´t seem to be opening any window to ask for it.
I´m always getting false in registerForActivityResult().
// Permission to get photo from gallery, gets permission and produce boolean
private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
new ActivityResultCallback<Boolean>() {
#Override
public void onActivityResult(Boolean result) {
if(result) {
Log.e(TAG, "onActivityResult: PERMISSION GRANTED");
} else {
Log.e(TAG, "onActivityResult: PERMISSION DENIED");
}
}
});
// Launch the permission window -- this is in onCreateView()
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPermissionResult.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
});
This is my LOG always: onActivityResult: PERMISSION DENIED
UPDATE
This answer works, but I found a better solution for permission requests with no open holes here.
From docs:
In your Activity/Fragment, create this field:
// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher, as an instance variable.
private ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new RequestPermission(), isGranted -> {
if (isGranted) {
// Permission is granted. Continue the action or workflow in your
// app.
} else {
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
});
Somewhere in the same Activity/Fragment:
if (ContextCompat.checkSelfPermission(
context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
performAction(...);
} else if (shouldShowRequestPermissionRationale(...)) {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a "cancel" or "no thanks" button that allows the user to
// continue using your app without granting the permission.
showInContextUI(...);
} else {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
If you are getting unreasonable "Permission denied" all the time, maybe you did not declare it in your manifest.xml?
Looking at Update to androidx.fragment:fragment:1.3.0-alpha08: registerForActivityResult not allowed after onCreate anymore. How to use after onCreate?,
private lateinit var checkLocationPermission: ActivityResultLauncher<Array<String>>
// Initialize checkLocationPermission in onAttach or onCreate.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkLocationPermission = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
if (permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true ||
permissions[Manifest.permission.ACCESS_COARSE_LOCATION] == true) {
initUserLocation()
} else {
// Permission was denied. Display an error message.
}
}
}
fun showMap() {
if (ActivityCompat.checkSelfPermission(requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(requireContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
initUserLocation()
} else {
checkLocationPermission.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION))
}
}
private fun initUserLocation() {
googleMap?.isMyLocationEnabled = true
}

java.lang.SecurityException: was not granted this permission: android.permission.WRITE_SETTINGS

I going to change some system setting in android and i use this code :
ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
this code used for change screen Brightness use Brightness sensor,
but in android 6 I get this exception
java.lang.SecurityException: com.vpn.sabalan was not granted this permission: android.permission.WRITE_SETTINGS.
i can use this method to get permission from user , but i need get permission programmetically can any one help me ?
private void showBrightnessPermissionDialog( )
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !android.provider.Settings.System.canWrite(this))
{
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:"+getPackageName()));
startActivity(intent);
}
}
Update Android Marshmallow and Higher
You can start System settings to grant Write System Settings. Once this permission is grant by user you can set brightness without any issues
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
}
Follow the step by step provided in documentation. It is very thorough.
https://developer.android.com/training/permissions/requesting.html
All you have to do is request permission, and override the callback for onRequestPermissionsResult to check if you got it or not. If you did, then you are good to go. You still need it in your manifest though or it won't work.
UPDATE to show details based on your comments.
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
private static final int REQUEST_WRITE_PERMISSION = 1001;
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doFileWork();
}else{
//handle user denied permission, maybe dialog box to user
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
} else {
doFileWork();
}
}
}
There are also many good libraries out there that wrap this callback context if you really want to go that route, but it isn't that complex. Make sure you also have write permission in your Manifest.

Android Default Video Camera Intent Opens Image Camera

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

Camera Intent keeps crashing Android App

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.

Categories