I have this code:
private void requestPermissionAndExport() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
try {
export();
} catch (IOException e) {
e.printStackTrace();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public void export() throws IOException {
String csv_data = "testtest";
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
root = new File(root, "my_csv.csv");
try {
FileOutputStream fout = new FileOutputStream(root);
fout.write(csv_data.getBytes());
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
It works when I first install and launch the app. If the user deletes the csv file from the download folder and try to open the app again, and export csv again, nothing happens. Also if I generate every time differents name for the file, only the first one (after installation of the app) is created.
Why does this work only in the first instance?
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
try {
export();
} catch (IOException e) {
e.printStackTrace();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Probably that's why. You call export() only inside onRequestPermissionsResult() and app asks for permissions only once, and remember it till you uninstall it. Try it yourself: if you clear app data from app manager it will ask again, and then export file again too.
Related
In our android webview app, We ask for read_contacts, location, camera, audio permissions at a time on initialization of app. What happens is that simultaneously the webview url is also loaded. This causes some crucial data like location not to be passed to webview in the first load.
What we expect from the app is to load the webview url immediately after user allows, grants permissions for the above only and not before that. We tried using onRequestPermissionsResult for achieving this, but unable to do so. The code we have tried is as given hereunder
if (!check_permission(4) || !check_permission(3)) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_PHONE_NUMBERS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.RECORD_AUDIO,Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
contact_perm);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
get_location();
asw_view.loadUrl(url);
}
}
}
Any help would be appreciated.
You're checking only for 0th element, i.e., grantResults[0] == PackageManager.PERMISSION_GRANTED in onRequestPermissionsResult(...).
You need to check like this:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == 1) {
// Use this method to check if all the permissions are GRANTED.
if (areAllPermissionsGranted(grantResults)) {
get_location();
asw_view.loadUrl(url);
} else {
/*
* NOTE:
* -----
* Add a Log here to check if all the permissions are granted.
* If this block doesn't executes, it means all the permissions are granted,
* something else is wrong inside your 'if' block and you need to debug that block.
* */
}
}
}
// Method to check if all the results are GRANTED.
private Boolean areAllPermissionsGranted(int[] grantResults) {
boolean isGranted = false;
if (grantResults.length > 0) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
isGranted = true;
} else {
// if a single permission is NOT_GRANTED, return from the method.
return false;
}
}
}
return isGranted;
}
Use this library
gradle:
implementation 'com.nabinbhandari.android:permissions:3.8'
then:
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; //add your requested permissions to this array
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
.setRationaleDialogTitle("Info")
.setSettingsDialogTitle("Warning");
Permissions.check(this/*context*/, permissions, rationale, options, new
PermissionHandler() {
#Override
public void onGranted() {
// do your task here
}
#Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied, block the feature.
}
});
I'm trying to write a program that will need tot save to external storage. Right now I cant seem to save to the external storage.
I included the necessary permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
My code is a simple test for writing to external storage.
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String path = Environment.getExternalStorageDirectory() + "/Pictures/" + "test.jpg";
File file = new File(path);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
I have run this code on an emulator and a physical device. In neither case do I see a file saved to the Pictures Folder. Really need help with this.
Since Android M+ you need to request permission not only in Manifest but at runtime also
private void requestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat
.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
//your permission was already granted then write to storage
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
// here you write to the storage for the first time
} else {
// Permission Denied
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
I am writing an app that will send a message to an inputted number through SMS. However when I try to send the message I get the error that "User 10074 does not have android.permission.SEND_SMS" even though I have this permission in my Manifest.
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("5554", null, "hello", null, null);
// smsManager.sendTextMessage(number,null,matn,null,null);
Toast.makeText(Sms.this, "OK", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(Sms.this, "Error"+e, Toast.LENGTH_LONG).show();
}
}
});
and code in manifest
<uses-permission android:name="android.permission.SEND_SMS" />
Please try below code with runtime permission.
call checkAndroidVersion("5554"); from your click listener
public void checkAndroidVersion(String mobile){
this.mobile= mobile;
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
return;
}else{
sendSms(mobile);
}
} else {
sendSms(mobile);
}
}
private void sendSms(String mobileNo){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mobileNo, null, "hello", null, null);
// smsManager.sendTextMessage(number,null,matn,null,null);
Toast.makeText(Sms.this, "OK", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(Sms.this, "Error"+e, Toast.LENGTH_LONG).show();
}
}
Also Override onRequestPermissionsResult method
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case SEND_SMS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSms(mobile);
} else {
Toast.makeText(Sms.this, "SEND_SMS Denied", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
try this to ask run time permission
requestSmsPermission();
private void requestSmsPermission() {
String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
I have an app that uses camera it works fine when I compile it with targetSdkVersion 23, but when I try to use version 25 I get this error:
android.os.FileUriExposedException:
file:///storage/emulated/0/DCIM/IMG_1093948364.jpg exposed beyond app
through ClipData.Item.getUri()
This is the code that I'm using:
private void showCameraAction() {
if(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
getString(R.string.mis_permission_rationale_write_storage),
REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
}else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
try {
mTmpFile = FileUtils.createTmpFile(getActivity());
} catch (IOException e) {
e.printStackTrace();
}
if (mTmpFile != null && mTmpFile.exists()) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
startActivityForResult(intent, REQUEST_CAMERA);
} else {
Toast.makeText(getActivity(), R.string.mis_error_image_not_exist, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), R.string.mis_msg_no_camera, Toast.LENGTH_SHORT).show();
}
}
}
private void requestPermission(final String permission, String rationale, final int requestCode){
if(shouldShowRequestPermissionRationale(permission)){
new AlertDialog.Builder(getContext())
.setTitle(R.string.mis_permission_dialog_title)
.setMessage(rationale)
.setPositiveButton(R.string.mis_permission_dialog_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{permission}, requestCode);
}
})
.setNegativeButton(R.string.mis_permission_dialog_cancel, null)
.create().show();
}else{
requestPermissions(new String[]{permission}, requestCode);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == REQUEST_STORAGE_WRITE_ACCESS_PERMISSION){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
showCameraAction();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
What should I do? Thanks.
Your FileUtils.createTmpFile(getActivity()); probably uses file:// URI to share file with other app(in your case camera).
Android versions greater then 24 use content:// URIs instead, and will throw this exception when you try to share a file directly using the file:// URI.
A content URI allows you to grant read and write access using temporary access permissions
Take a look at FileProvider.
SOLUTION:
Changed this:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
To this:
//getActivity() because its a fragment
Uri uri = FileProvider.getUriForFile(getActivity(),
getActivity().getPackageName()
, mTmpFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
And it worked.
I have an issue only on Asus Nexus 7 2013. All other devices, including Asus Nexus 7 2012 works fine.
This device has 2 camera IDs: 0, 1.
But I can't open camera, using both of them.
//...
try {
//cameraID: 0 or 1
camera = Camera.open(cameraID);
//...
} catch (Exception e) {
e.printStackTrace();
}
So I getting:
java.lang.RuntimeException: Fail to connect to camera service
android.hardware.Camera.<init>(Camera.java:495)
android.hardware.Camera.open(Camera.java:341)
Problem was in new Android 6+ feature: Request Permission at Runtime.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!permissionRequestInProgress) {
permissionRequestInProgress = true;
new Handler().post(new Runnable() {
#Override
public void run() {
if (ContextCompat.checkSelfPermission(RootActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(RootActivity.this,
new String[]{Manifest.permission.CAMERA},
CAMERA_PERMISSION_REQUEST);
} else {
//we got it
}
}
});
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case CAMERA_PERMISSION_REQUEST:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
//we got it
} else {
// permission denied
}
permissionRequestInProgress = false;
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}