I have been stuck on this issue for some time. I am trying to make my app request 2 permissions at once, as so:
ibTakePicture.setOnClickListener(new View.OnClickListener() {
//this get image and location and then display both
#Override
public void onClick(View v) {
ActivityCompat.requestPermissions(
HomePageActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA},
REQUEST_CODE_MULTIPLE_PERMISSION
);
}
});
So the idea is when the button is clicked the user is prompted to give location and camera permissions, so I can then store the location and photo somewhere. The camera functions work fine, but I am still stuck on getting the location. here is my onRequestPermissionsResult:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
getCurrentLocation();
dispatchTakePictureIntent();
} else {
Toast.makeText(
HomePageActivity.this,
"Location and Camera permissions must be granted for this app to work.",
Toast.LENGTH_LONG
).show();
}
}
}
I am pretty sure it's just a problem with these methods, any help would be massively appreciated.
I have tried other ways, such as in my previous question Java Android - Requesting multiple permissions but one doesn't get requested
When requesting ACCESS_FINE_LOCATION you also need to ask for ACCESS_COARSE_LOCATION.
To handle this potential user behavior, don't request the
ACCESS_FINE_LOCATION permission by itself. Instead, request both the
ACCESS_FINE_LOCATION permission and the ACCESS_COARSE_LOCATION
permission in a single runtime request. If you try to request only
ACCESS_FINE_LOCATION, the system ignores the request if you app target Android 12 or above.
Blockquote
AndroidManifest.xml
<manifest ... >
<!-- Always include this permission -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Include only if your app benefits from precise location access. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
Java code
ActivityCompat.requestPermissions(HomePageActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA},
REQUEST_CODE_MULTIPLE_PERMISSION
);
You can find more info on google docs - Request location permission
Related
i am trying to create a video player ,So I am trying to add the videos to the list
Storage permission is required to fetch the videos, so I took the permission with the below code.
But playstore was reject My app for this MANAGE EXTERNAL STORAGE permission.
But without this permission, I can't get storage permission on Android 10+ device.
To change the name of the video, delete the video and download the video permission is required , so please help me , please tell me how to get storage permission (/storage/Media/Videos , /storage/Download/)
My storage permission code :-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
Main activity code :-
private boolean checkPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int result = ContextCompat.checkSelfPermission(PermissionActivity.this, READ_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(PermissionActivity.this, WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2296);
}
} else {
//below android 11
ActivityCompat.requestPermissions(PermissionActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2296) {
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean READ_EXTERNAL_STORAGE = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean WRITE_EXTERNAL_STORAGE = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (READ_EXTERNAL_STORAGE && WRITE_EXTERNAL_STORAGE) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
So please tell me how to take storage permission in Android10+ Devices and also below Android 10 devices with out using MANAGE EXTERNAL STORAGE permission , Please Help Me
Permissions have changed in Android 10+:
External storage access scoped to app files and media
By default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage. Such apps can see the following types of files within an external storage device without needing to request any storage-related user permissions [..]
Source: Privacy changes in Android 10
On devices that run Android 10 or higher, you don't need any storage-related permissions to access and modify media files that your app owns, including files in the MediaStore.Downloads collection
Source: Storage Permissions
If you have
android:requestLegacyExternalStorage="true"
in application tag in manifest file then you are done for an Android Q/10 device.
It will behave as it behaved on before 10.
I do not understand why you would have any problem on Android 10.
On Android 11+ you should be able to see media files in the usual public directories.
Hi So Recently I started coding in android studio and I included dexter for permissions to access user storage but the main concern is that the code is directly showing the message of denied block even without asking for permiisions.PLease help
Dexter.withContext(this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
#Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
Toast.makeText(MainActivity.this, "Thanks !", Toast.LENGTH_SHORT).show();
}
#Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
Toast.makeText(MainActivity.this, "not granted", Toast.LENGTH_SHORT).show();
}
#Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
})
.check();
maybe the case that your application doesn't have the permission yet.
Since android 6, some permissions are considered dangerous permissions and even if you added the required permission to your android manifest, you shouldn't assume that your application have it yet and you should always ask for permissions at runtime before proceeding with the actions that require them.
if you didn't add the READ_EXTERNAL_STORAGE to the android manifest, it should look like this :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
...
</application>
</manifest>
if you did add it but still access to external storage is denied then this is how to ask for it at runtime :
First initialize an ActivityResultLauncher and register it as an activity result which must be before {initialization, onAttach(), or onCreate()} fragment creation methods, so the only way was to initialize it as a global fragment variable
Then when you need to start using external storage, you first use the ActivityResultLauncher you initialized before to launch a request for permissions needed.
If permissions was granted the callback should continue your work flow naturally, else (temporarily) you should show a toast/Alert dialog message telling user why he can't continue with his action.
public class myFragment extends Fragment {
...
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted){
//continue your work flow
goDoSomething();}
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.
Toast.makeText(getContext(), "Can't continue without the required permissions", Toast.LENGTH_LONG).show();
}
});
public void getNecessaryPermissionsAndDoSomething() {
if (ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//check first if you have the permission or not
//if you don't then launch permission request dialog
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
}else goDoSomething(); //continue your work naturally
}
}
for more information about this you should check the android developers documentation about requesting permissions at runtime.
update
if (ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
Log.e("permission","not granted");
else Log.e("permission","granted");
I am new in android and was trying to test something. I have this simple app, where I just want to read files from any android folder. Till now, I just checked how the paths are there which I came to know from the following
context.getExternalFilesDir(null);
&
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
The paths obtained are like
/storage/emulated/0/ then rest of the folders
I have done everything, asked for permission in the manifest file, and in the app too. I check that even though the permission is granted and folder path is also correct but the File.listFiles() is givingme null pointer exception.
Please take a look at the code below:
Permission in manfest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Code to get permission and try checking the files
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},101);
Toast.makeText(getApplicationContext(),
"No permission yet",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),
"Permission already granted",
Toast.LENGTH_SHORT).show();
File dcimPath = new File("/storage/emulated/0/bluetooth"); //This path is correct
File[] files = dcimPath.listFiles(); // gives null pointer exception
Log.i("files length ",files.length+"");
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 101){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(),
"Permission granted",
Toast.LENGTH_SHORT).show();
File dcimPath = new File("/storage/emulated/0/bluetooth/");
File[] files = dcimPath.listFiles(); //gives error
Log.i("files length ",files.length+"");
}
else{
Toast.makeText(getApplicationContext(),
"Permission denied",
Toast.LENGTH_SHORT).show();
}
}
}
ERROR
Caused by: java.lang.NullPointerException: Attempt to get length of null array
Try adding this line to the Android Manifest File
android:requestLegacyExternalStorage="true"
This is used to read Shared Storage apart from the app's private storage. Read this if you are curious to know more...
Hope this helps. Feel free to ask for clarifications...
I need to access the user's location, and the permission for it is crucial for my app.
I tried this code but it did not ask for permission the second time (and so forward):
while(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
}
If the permission is really crucial to continue in the app, as far as I know, the only think you can do is just to finish the app when you get in the permissions callback that this permission has not been granted.
This way the next time the app is opened it will ask again for the location permission.
We check for permissions at the main activity and show a toast that says that the permissions are required and finish the main activity in one app which makes no sense using it without location. And the app is published in the google play store.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (permissions.length > 0 && requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setUp();
} else {
Toast.makeText(this, R.string.permissions_required, Toast.LENGTH_LONG).show();
finish();
}
}
}
Even though it might be better if the rest of the app could be used without location, but sometimes there is no other way and if you try to ask for the same permission once and again the dialog will not show up at some point.
It is a very strange situation. I am working with camera2 API and there is a regular method to open the camera.
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
this method is requiring to make test, this one
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.
return;
}
it is simple test to deduce if there was declared CAMERA permission in manifest or not.
<uses-permission android:name="android.permission.CAMERA" />
i have this permission in my manifest file and if i am uploading this app to Samsung S5 it is work properly without problem, but if i am uploading this app to chinese device, mistake is happen. Don't pass the test and eventually don't open the camera...
Maybe should i set permission dynamically?
And one more thing, i have tryed call method to open the camera inside test, this way
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
return;
}
but get this mistake
FATAL EXCEPTION: main Process: com.example.android.camera2basic, PID: 29649 Theme: themes:{} java.lang.SecurityException: Lacking privileges to access camera service at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:108) at android.hardware.camera2.legacy.CameraDeviceUserShim.connectBinderShim(CameraDeviceUserShim.java:336) at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:327) at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:457) at com.example.android.camera2basic.activities.CameraActivity.openCamera(CameraActivity.java:919)
What i am doing wrong?
On API level 23+ you need to request permissions at runtime (even if you have them declared in your Manifest).
You should do something like this:
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
CAMERA_REQUEST);
To process the result you need to override onRequestPermissionsResult() in your Activity:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == CAMERA_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
// you can do your Camera related stuff
} else {
// permission denied
}
}
// ...
}
Check out the documentation on runtime permissions.