Not asking external storage permission on Android studio - android

my name is Tom, i am trying to open a file from the external storage. for some reason it is not asking the permission at all?!?!
I will thank for any answer!
Tom.
Here is my code:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this,"should snd request ",Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUET_CODE);
}
public void OnRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResult){
if(requestCode == PERMISSION_REQUET_CODE){
if(grantResult[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this,"permission granted!",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"*** permission not granted! ***",Toast.LENGTH_LONG).show();
finish();
}
}
}

Make sure you also have added permissions to the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appname">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>

Related

"Required permission was denied!" when requesting WRITE_EXTERNAL_STORAGE

I am trying to request WRITE_EXTERNAL_STORAGE permission on Android 10. I do that using the following code:
ArrayList<String> permissions = new ArrayList<>();
if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (this.checkSelfPermission(ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
this.checkSelfPermission(ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() > 0) requestPermissions(permissions.toArray(new String[0]), PERMISSION_REQUEST_FINE_LOCATION);
Here is the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
All other permissions work except WRITE_EXTERNAL_STORAGE. When I try to request it, it does not display any dialog for confirming permission, but it simply denies it. Why and how can I fix it?
I have discovered the solution, however the cause of the problem is still a mystery to me. If anyone has an explination, it would be greatly appreciated. Basically my issue is a duplicate of this issue. All I had to do is to change the following line from
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
to
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
What does tools:node="replace" do?
You can handle requested permission using this method (works with android 10), Here is the documentation.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) {
case PERMISSION_REQUEST_ID_STORAGE:
// permisstion granted by user
break;
}
} else {
switch (requestCode) {
case PERMISSION_REQUEST_ID_STORAGE:
// permission denied by user
break;
}
}
}
You can use Android Runtime Permission Library by nabinbhandari
in you build.gradle app module file write this
//Android Runtime Permission Library
implementation 'com.nabinbhandari.android:permissions:3.8'
In your Manifest file write
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and in Activity declare these variables
String[] permissions = new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE};
String rationale = "Please provide storage permission to proceed ...";
And in your onCreate() Method write this
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(MainActivity.this, permissions, rationale, options, new PermissionHandler() {
#Override
public void onGranted() {
// do your task.
Toast.makeText(MainActivity.this, "Permissions granted", Toast.LENGTH_SHORT).show();
}
#Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied
Toast.makeText(MainActivity.this, "Permissions denied", Toast.LENGTH_SHORT).show();
}
});

Marshmallow permission

I'm trying to check permission in activity as below,
int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNT);
But I'm getting error at GET_ACCOUNT as Cannot resolve symbol 'GET_ACCOUNT'.
Kindly help what code should I add to fix this?
Thanks in advance.
It should be
Manifest.permission.GET_ACCOUNTS
instead of
Manifest.permission.GET_ACCOUNT
Try like this
int permission = ContextCompat.checkSelfPermission(this,Manifest.permission.GET_ACCOUNTS);
Don't forget to add permission on your Manifest
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
Import this library in your class
import android.Manifest;
First GET_ACCOUNT is not defined in android.Manifest.permission, it is GET_ACCOUNTS
Second, change your Manifest file to use GET_ACCOUNTS
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
then in your code file i.e. java file write like this.
ContextCompat.checkSelfPermission(this, android.Manifest.permission.GET_ACCOUNTS);
Check documentation GET_ACCOUTNS.
The following code worked for me,
Declare below code as:
public static final int PERMISSIONS_REQUEST_GET_ACCOUNT = 133; // any number
Call following code in your onCreate method:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.GET_ACCOUNTS},
PERMISSIONS_REQUEST_GET_ACCOUNT);
}else{
//furtherCode
}
}else{
//furtherCode
}
in method onRequestPermissionsResult method,
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case PERMISSIONS_REQUEST_GET_ACCOUNT :
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Granted", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "No such permission to access account!", Toast.LENGTH_SHORT).show();
}
break;
}
}

vuforia sdk + android failed to initialize Vuforia with permission exception

App crashes after running the program with failed to initialize
Vuforia with permission exception
Android version is <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
testing on device 4.1.1 (api level 16) with front camera only.
Permission included in manifest file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:glEsVersion="0x00020000" />
exception at SampleApplicationSession's InitVuforiaTask Task, value of Vuforia.init() returned is -1.
Not sure what I missed.
Library included are armaebi-v7a/libVuforia.so, android-support-v4, jpct_ae, Vuforia
I have faced the same problem. If you see the example comes with the compiledSdKversion 22 because in newer versions the user have to explicitly give the Camera permission. My project is working with API 25 by adding some code to my android application. In my case, I asked for the Camera Permission before opening the vuforia activity when an user clic a FloatingActionButton:
FloatingActionButton flb=(FloatingActionButton)findViewById(R.id.floatingActionButton2);
flb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
else
{
Intent myIntent = new Intent(MainActivity.this, VideoPlayback.class);
startActivity(myIntent);
}
}
});
The VideoPlayback is the activity that use AR from vuforia included in the advance examples. In this case you have to listen to an onRequestPermissionsResult because we have to check the user's answer.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
// Begin monitoring for Aruba Beacon-based Campaign events
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Intent myIntent = new Intent(MainActivity.this, VideoPlayback.class);
startActivity(myIntent);
}
}
}
In the onRequestPermissionsResult we check if the answer was positive an if so we open the activity.
I hope it works for you too.

Doesn't work on Android Studio- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

I'm trying to find make the following line work:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
The studio marks it as Error and suggests to check that all permissions are set or handling a SecurityException.
I think that I have set all the necessary permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
But still , it does not work.
Did someone have the same problem ?
Thanks in advance !
you can use these code.In android studio you have to check dynamic permission at run time
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
getLocation();
}
and
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
}
break;
}
}
This is because in Marshmallow, permissions are asked at runtime to the user.So we need to handle wheather user has granted that particular permission or not.
Add these two permissions:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>

onRequestPermissionsResult returns immediately with denied permission

My Android app needs to request permission for location services. I do this with:
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_CODE_ACCESS_COARSE_LOCATION);
But immediately after this is called, onRequestPermissionsResult returns immediately with permission denied:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ACCESS_COARSE_LOCATION: {
}
}
}
The permission is listed in the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
I also cleared the cache to prevent any previous denial the user gave from affecting this.
I am expecting a dialog to pop up requesting the user to grant or deny permission to the location services but it isn't showing.
Change:
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
to:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
The permission is for "coarse" location data (i.e., not fine-grained), not "course" location data (e.g., where some university class is being held).
private int PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 100;
private void checkPermission() {
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(newString[{Manfest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
} else {
doShowLocation();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showContacts();
}
}
One more scenario that onRequestPermissionsResult can return denied permission even before permission dialog appears is when you declared some other permission in android manifest and asking some other permission in run time. Example:
You have declared
<uses-permission android:name="android.permission.READ_SMS" />
in android manifest file. However you are trying to get
Manifest.permission.SEND_SMS
in runtime permission. So making the runtime permission to READ_SMS will work without any issues.

Categories

Resources