permission in java android grandResult - java

I am really looking for the use of this condition (grandResult.lenght() > 0) in android permission
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case WRITE_EXT_RQS_CODE:
if (grandResult.lenght() > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
createFile();
Toast.makeText(this, "permission is granted , length : " + grantResults.length, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "permission is denied length : " + grantResults.length, Toast.LENGTH_SHORT).show();
}
}
}
I am not understanding why grandResult.length > 0 is written in this condition,
android said that when canceling the request will return to the grandResult array 0,
but when I click on deny it return 1.
the question is why we put grandResult.length() > 0 condition.

Related

How to add files access permission on device android

My problem is when I am not getting files permission in the device.
Below image is showing: photos and media access are showing but files access not showing.
In the above permission access files permission not showing like below: I need below permissions including files access.
I use permision in the manifest:
<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" />
From the MainActivity:
private static final int CAMERA_PERMISSION_CODE = 100;
private static final int STORAGE_PERMISSION_CODE = 101;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = findViewById(R.id.storage);
camera = findViewById(R.id.camera);
// Set Buttons on Click Listeners
storage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
STORAGE_PERMISSION_CODE);
}
});
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
checkPermission(Manifest.permission.CAMERA,
CAMERA_PERMISSION_CODE);
}
});
}
// Function to check and request permission.
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, permission)
== PackageManager.PERMISSION_DENIED) {
// Requesting the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { permission },
requestCode);
}
else {
Toast.makeText(MainActivity.this,
"Permission already granted",
Toast.LENGTH_SHORT)
.show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions,
#NonNull int[] grantResults)
{
super
.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Camera Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Camera Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Storage Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Storage Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions,
#NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Storage Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Storage Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
Changing the targetSdkVersion from 30 to 29 in my build.gradle file changed the dialog for me from photos/media to photos/media/files. I believe it has something to do with how API 30 is more strict with scoped storage.
Adding the below line in the Application portion of Manifest
android:requestLegacyExternalStorage="true"
solves the problem.

How to run camera after permission accepted

I have fragment that contain method that running camera.
But , when user enter at the first time to the app and going to this fragment, I ask camera permission.
And after user Accept the permission to use camera, camera is not starting.
Only after he is exiting from this fragment and going back to it, the camera will run.
I need some listener for the permission accept i think.
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You can implement a PermissionResult listener as:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
cameraSource.start(surfaceView.getHolder());
else
Toast.makeText(context,"Permission denied",Toast.LENGTH_LONG).show();
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
If you're asking for more than one permission, you can check it as:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
//Now check each permission as
if (permission.equals(Manifest.permission.CAMERA)) {
//do something like checking granted result
}
else if (permission.equals(Manifest.permission.LOCATION)) {
//do something
}
}

After accept permission how to open a activity?

I trying to do a simple functionality. After the user accept permission for camera i need to open a new activity, but how do this?
This is a code. I check the permission and the request it. On my else if i open a new activity.
#RequiresApi(api = Build.VERSION_CODES.M)
#OnClick(R.id.scanButton)
void scanZxing(View view) {
if(getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(context, "We need permissions to acces your CAMERA!", Toast.LENGTH_SHORT).show();
((MainActivity) context).checkCameraPermission();
} else if (view.getId() == R.id.scanButton) {
Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
startActivityForResult(intent, 0);
}
}
#TargetApi(23)
public void checkCameraPermission(){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
return;
}
if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "We need your permission to open camera",
LENGTH_SHORT).show();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
You can check the permission request result in your activities onRequestPermissionsResult methodm and if user granted the permission then open the activity
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_CAMERA &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permisssion was granted
} else {
//permisssion was not granted
}
}
I am supposing you want to open ScanBarcodeActvity.
you can do it in following way
public void openScanBarCodeAcitvity() {
Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
startActivityForResult(intent, 0);
}
Call above method at following place in addition to scanZxing() method
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
//permission granted, open activity
openScanBarCodeAcitvity();
} else {
Toast.makeText(this, "We need your permission to open camera",
LENGTH_SHORT).show();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}

Ask for runtime permission only for Android Marshmallow?

I want to add run time permission only for marshmallow because it crashes my application in Android marshmallow devices otherwise its working properly. Is it possible to add multiple permissions at a single time? I have written code for camera permission, but
I have written code for single permission and that is working fine.
private void requestpermissioncamera()
{
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_CAMERA)) permissionsNeeded.add("Camera");
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),PERMISSION_REQUEST_CODE);
return;
}
this function is used to check weather permission is granted or not.
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
Of course it is. You can see an example of how it's implemented below:
public String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public boolean verifyStoragePermissions(Activity activity, int REQUEST_EXTERNAL_STORAGE) {
initialize(activity);
// Check if we have write permission
int permission =
ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
return false;
} else {
return true;
}
}
for my project i made Utils class with this method:
public static boolean checkAllAndRequestPermisions(Activity activity, int requestCode, LinkedList<String> permissionsList) {
LinkedList<String> needPermissions = new LinkedList<>();
for(String permission : permissionsList){
if (ContextCompat.checkSelfPermission(activity, permission) != activity.getPackageManager().PERMISSION_GRANTED){
needPermissions.add(permission);
}
}
if (!needPermissions.isEmpty()){
ActivityCompat.requestPermissions(activity, needPermissions.toArray(new String[needPermissions.size()]), requestCode);
}
return true;
}
And implement onRequestPermissinsResult interface like this:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case Consts.SOME_PERMISSIONS_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(getApplicationContext(), "Need permissions", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Its not perfect solution, but ok for first time, i hope its help.
Also dont forget, you can set in gradle targetSdkVersion less then 23 to have time for implement all requests permissions functionality.
It is easy to get multiple permissions at a single shot.
I hope below code can help you out for granting multiple permissions.
private void requestpermissioncamera()
{
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("GPS");
if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
permissionsNeeded.add("Read Contacts");
if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS))
permissionsNeeded.add("Write Contacts");
if(!addPermission(permissionsList,Manifest.permission.CAMERA))
permissionsNeeded.add("Camera");
if(!addPermission(permissionsList, Manifest.permission.ACCESS_NOTIFICATION_POLICY))
permissionsNeeded.add("push Notification");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
{
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_NOTIFICATION_POLICY, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED)
{
// All Permissions Granted
// insertDummyContact();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

Displaying a TextView depending on whether Android permission is disabled?

I'm trying to display a TextView which appears visible only when a particular Android permission, i.e. WRITE_EXTERNAL_STORAGE, is not enabled. The idea is to explain to the user why they need to enable it in case they've disabled it the first time, and then have them click the TextView to launch the permission request again. The following is not working for me. How can I accomplish this? I have this in my onCreate() method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
if (NetworkHelper.isOnline(this)) {
new AppendParseData().execute();
}
}
TextView checkFilePermission = (TextView) findViewById(R.id.checkFilePermission);
if (!checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
checkFilePermission.setVisibility(View.VISIBLE);
}
try this way when you request for permission(I have used READ_CONTACTS permission you can use WRITE_EXTERNAL_STORAGE instead)
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
requestContactsPermissions1();
} else {
setTextVisablity(false);
checkSimCard();
}
in onRequestPermissionsResult
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
readContacts();
setTextVisablity(false);
} else {
// Permission Denied
setTextVisablity(true);
Toast.makeText(MainActivity.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
in the end setTextVisablity method
public void setTextVisablity(boolean isShow) {
if (isShow) {
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
Use this code:-
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
return;
}
else{
//Permission already granted show textview here
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Toast.makeText(MainActivity.this, " PERMISSION_GRANTED", Toast.LENGTH_SHORT)
.show();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, " Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Categories

Resources