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/*
Related
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 want to get the location from Android, so I set a permission. But I got confused when I found another article saying:
android.permission.INTERNET is needed.
Why is it so? Does ACCESS_FINE_LOCATION include INTERNET?
INTERNET permission allows you to connect to the internet. Without it, all attempts will fail or throw an exception. FINE_LOCATION allows you to use GPS. It does not include INTERNET.
Is it necessary to use "ACCESS_NETWORK_STATE" permission to work "CONNECTIVITY_CHANGE" reciver?
When I test it with "INTERNET" permission and not "ACCESS_NETWORK_STATE" works like a charm.
Any idea or comment will be appreciated.
android.permission.INTERNET
Allows applications to open network sockets
&&
android.permission.ACCESS_NETWORK_STATE
Allows applications to access information about networks
It do requires the caller to hold the permission ACCESS_NETWORK_STATE
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.
Most of the permission checking will happened in checkPermission or checkUidPermission in current Android permission framework.
But the android.permission.INTERNET permission will not be checked in these two methods. So I wondering about the exactly checking function/method of this permission in Android.
I've checked the code.
The system permission like INTERNET or file operations will be controlled by gids with the user id. The uid without the net gid will not be able to create the socket and will return EACCES(permission denied) by linux kernel.