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.
Related
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/*
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
I have an Android Cordova app and I'm using GPS, check the network state, read/write on the Documents folder and taking camera pictures. Here my permissions on the manifest XML file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The manifest file is auto generated by the Cordova framework. For some reason I don't see camera permissions. Permissions are not asked at installation time anymore (that's since Android 6) but instead they should be asked before usage.
I correctly get the GPS access permission popup but not the read/write Documents folder permission. I also never get the camera permission albeit I'm able to use it without ever being asked for permission. Same story for the Network status permissions (never being asked).
I find Android permissions scheme extremely confusing, under application manager my app has got Location and Storage as expected, Camera and Network status are missing though.
To recap, inside the app, on the actual code, I'm using at least once those devices
GPS fine grained
GPS coarse (probably the Wifi SSID triangulation trick)
Write on Documents
Read on Documents
Read network status (Offline / Wifi / 3G etc..)
Take picture from the camera
Cordova framework wrote this manifest file:
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.hardware.location.gps (why is it not a .permission?)
android.permission.ACCESS_NETWORK_STATE
android.permission.WRITE_EXTERNAL_STORAGE
On application manager I get those options:
Location
Storage
So basically I get three different sets of permissions :-(
I found this in the this cordova plugin that you mentioned in comments.
So somehow this <uses-permission android:name="android.permission.CAMERA/> have to be in your code so as this plugin can use it.
Maybe you didn't check the right program to see its permissions, or if the camera permission is not shown in application permissions on device, you can't be able to use camera in this application. Please do a check again because I really want to know what is the situation.
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 ?
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" />