Android 6 Write permission fails to the extension SD card - android

I am failing to get the Android 6 write permission to the memory extension SD card. I am getting the exception "Permission denied”. I am using Android 6, targetSdkVersion 24, with both manifest uses-permission request and run time permission request for WRITE_EXTERNAL_STORAGE. I am getting granted ok permission replay on the onRequestPermissionsResult().
I am able to read from this SD card but I am failing to write to it. I am using a Samsung note 4, 6.0.1. I am trying to write to the file: “/storage/3530-3464/my_file.txt”, I can read this file with the same path. I am getting exception on this line:
File file = new File(SD1_StoragefilePath);
FileOutputStream fop = new FileOutputStream(file); // get permission exception here.
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Runtime permission:
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQ_REPLAY_WRITE_EXTERNAL_STORAGE);

thanks for your help greenapps.
using the SAF as implemented:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_STORAGE_ACCESS);
I can have write access to the entire SD card.
There are two issue:
When implementing the intent command a funny card selection activity spears, is there a way to get read of this selection activity, and by default select the SD root.
Intent.ACTION_OPEN_DOCUMENT_TREE activity
On every Application start this activity spears.
Is there a way to remember this permission granted ?

Related

Permission denied while reading a system file on Android

I am trying to add "HDMI connection" check in my app.
I referred to How to check the HDMI device connection status in Android? and decided to add the checks to read /sys/devices/virtual/switch/hdmi/state or if not, /sys/class/switch/hdmi/state files.
But I am getting:
java.io.FileNotFoundException
with "Permission denied". Do we need to request some permissions for this kind of access?
Thanks,
Vinay
I stumbled across the same problem.
resolve permissions problem
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
READ should be enough but if you grand Write works the same.
resolve runtime permmision problem - if you work with API 23+ you have to ask permission on runtime.
Root device - I had to root device to access sys/class/switch/*

Can't create folder in external public storage

I am trying to simply create a subdirectory in the external storage pictures directory, however, I always end up in the lower if condition. (The directory does not exist and mkdir fails creating it).
...
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "foto_test_app");
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
Log.e(LOG_TAG, "External Storage not mounted.");
return null;
}
if(!storageDir.exists() && !storageDir.mkdir())
{
Log.e(LOG_TAG, "Directory not created.");
}
...
I do have the the line in the Manifest file that reads:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I tested it on two different devices. Both do not have an SDCard installed, but that should not be a concern, right? What am I missing?
Probably you are using Android Marshmallow or above.
You need to make sure that you have granted run-time permission for storage.
This can be done either by setting it explicitly in the settings of your android device or asking for the permission during run time.
You can check below link how run time permission works
https://developer.android.com/training/permissions/requesting.html

Android Bluetooth permission not displayed, still descirbed as turned on

I have a problem with bluetooth permission. I've declared in the manifest following lines:
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
What is weird that in app settings instead of bluetooth I've got a right to add permission for Location and Storage. But ok, lets say android doesn't provide specific perms for bluetooth and these are the same as BT. Weird thing is that even when I don't give any permission to the app (not for memory, not for localization), by using following code:
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_ADMIN);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "not granted :(", Toast.LENGTH_SHORT).show();
}
I still get message that permission are granted. What the heck? Should I check specifically for a EXTERNAL_STORAGE and Location permissions? And why bluetooth isn't displayed in app permision configurations?
There are two types of permissions in Android:
Dangerous permissions: These permissions are required to ask on run time if targetSdkVersion is 23 or above.
Normal Permissions: If you define these permissions in Manifest that is enough.
For more details check this link given below:
https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

Environment.getExternalStorageDirectory() returns null int AndroidTestCase

I have a test case, and using there
Environment.getExternalStorageDirectory() call, and it returns null.
I have the "write external storage" permission in app's manifest (and sd cards works fine in app), but it does not work in test case. Why and how to fix it?
Device is SGS3, os version is 4.4.
you have to add the below permission in the Manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Not able to access picture files in android-Galaxy S3

I want to access the picture files in public folder in external storage. Here is my code:
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
java.io.File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
///storage/sdcard0/Pictures
boolean canRead = pictureDir.canRead();
//Above gives false
java.io.File[] picFiles = pictureDir.listFiles();
//null is returned for the file though I've atleast three picture files at this location
}
It gives me correct pointer to public folder, but canRead() method returns false. When I try listing the files under Picture directory, it gives me null.
Let me know if you need more details.
[UPDATE]
Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
[UPDATE 2]
This looks like a device specific issue since it works fine on emulator.
I'm just chiming in with another point of "device specific" troubles.. I have a ZTE Valet that I'm trying to get access to an external directory for reading, but having the same problem where the following permission results in getExternalFilesDir() to return null:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18"
/>
The trick mentioned above (use the WRITE_EXTERNAL_STORAGE permission) indeed results in a File being returned by getExternalFilesDir()... odd.. so odd...
You declare wrong permission in you AndroidManifest.xml. You are actually using external storage. From API 16 there is new permission that is required if you are using external storage:
http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE
You can test your app with the permission enforced by either running
your app on the Android Emulator when running Android 4.1 or higher,
or enabling Protect USB storage under Developer options in the
Settings app on a device running Android 4.1 or higher.
Probably in your device is set Protect USB storage in such case you need to use READ_EXTERNAL_STORAGE permission. By the way WRITE_EXTERNAL_STORAGE will cover READ_EXTERNAL_STORAGE.

Categories

Resources