I am trying to read files from an attached USB flash drive, I have scoured through countless Stackoverflow questions and nothing is working.
I am able to detect when a USB is attached and I know that the USB is mounted under
/mnt/media_rw/
and I'm able to get this path using StorageManager but the system denies my app permission to read any files under that path even after adding all the needed permissions to the manifest
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
According to the documentation "MANAGE_EXTERNAL_STORAGE" permission should give me access to all files on a storage device:
The MANAGE_EXTERNAL_STORAGE permission grants the following:
Read and write access to all files within shared storage.
Access to the contents of the MediaStore.Files table.
Access to the root directory of both the USB on-the-go (OTG) drive and the SD card.
Write access to all internal storage directoriesâ , except for /Android/data/, /sdcard/Android, and most subdirectories of /sdcard/Android. This write access includes direct file path access.
https://developer.android.com/training/data-storage/manage-all-files
My question is very simple, all I want is a way to read files from an attached USB flash drive
You should let the user choose the usb drive with ACTION_OPEN_DOCUMENT_TREE.
After that you can browse the whole drive and have access to all files.
Related
I have an issue with writing to sd card on my android 4.4 device. I'm writing an application that gets path to its directory on sd card using the getExternalFilesDirs() method. But when I try to store data in the directory I get an error:
EACCES (Permisson denied)
The applications directories in Android/data/ in both internal memory and on the sd card are owned by different linux users. Any other application on my device works fine. The application works on another device with that sd card.
You need to add this permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
i think you have missplaced this line.
Double check this line
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
where you have put this line?
this line should be outside of <application/> scope
like this
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>
For getExternalFilesDirs to return the path of the sdcard, the OEM must have set the SECONDARY_STORAGE environment variable in the device specific init.rc file as mentioned here: https://source.android.com/devices/storage/config-example.html
Look at the source of getExternalFilesDirs here: http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/app/ContextImpl.java#1039
The value is obtained from Environment.buildExternalStorageAppFilesDirs. Look at that source here: http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#206
The value is dependent on mExternalDirsForApp, which in turn is populated by reading the contents of SECONDARY_STORAGE variable: http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/Environment.java#136
As you can see, if the SECONDARY_STORAGE variable is not set, the sdcard path will not be returned. You can cross-check this by going to adb shell and looking at the output of echo $SECONDARY_STORAGE
use getExternalFilesDir()
It returns the path to files folder inside Android/data/data/your_package/ on your secondary storage SD card. It is used to store any required files for your app (e.g. images downloaded from web or cache files). Once the app is uninstalled, any data stored in this folder is gone too.
This is where you can write file in secondary storage ie(your micro sdcard)
if you want to write outside or the root of the secondary storage you have to use Storage access framework .
I am creating a FileManager app, where i am creating copy and cut functionality its working fine in internal device storage but when i try to copy something in external storage it says Permission denied. I have all the write permissions granted.
How can i write in external storage?
It depends what you mean by external storage. If you mean the external storage on the internal SD card, then
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in the manifest should be sufficient, assuming it is also granted in the App's security settings for Marshmallow and later.
On the other hand, if you mean an external removable SD card, then you will almost certainly not be able to write to it on KitKat or later unless you have taken specific steps to make it writeable.
There is a good summary of the state of play with removable SD cards here, which I suggest you take a look at:
How does storage access change on Android 6.
There are many questions about this topic, but I cannot find any answers for my corrective example.
I'm using Samsung galaxy S5 run android 4.4, which is limited for storage
The official document said:
The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.
My application need to write files to Sdcard (Absolute path is /storage/extSdCard), so I write my app data to my app directory: /storage/extSdCard/Android/com.myapp.example/files but got permission denied exception. So I suspect the above statement:
except in their package-specific directories as allowed by synthesized
permissions
I think I cannot write to root directory /storage/extSdCard but still able to write my app data to my app package directory. Did I misunderstand something here?
p/s: I still able to write my app data to built-in storage: /storage/emulated/0/Android/data/com.myapp.example/files. I don't want to use getExternalFileDirs() because it always return built-in, not my sdcard directory.
If getExternalFilesDir(null) is returning somewhere different to /storage/extSdCard/Android/com.myapp.example/files, then I would think that is why it is giving you access permission errors. The only place on the SD card you can write to without permissions is the directory returned by getExternalFilesDir()
Since you say the directory returned by getExternalFilesDir(null) is not acceptable, I would suggest adding the WRITE_EXTERNAL_STORAGE permission to your manifest.
Your app specific directory should be /storage/extSdCard/Android/data/com.myapp.example/files and not /storage/extSdCard/Android/com.myapp.example/files
Is it possible to write an android application than can gain access to files and documents on an android device and modify them (delete if possible), not necessarily resource files used by the OS but general user documents, provided that the user of the device allowed the application to do this?
PS. This is not for any unethical use but for academic purposes. :)
As long as you have the permission for it, you'll have no issues.
The OS segregates storage into two categories - Internal and External. Quoting the documentation:
External storage is the best place for files that don't require access
restrictions and for files that you want to share with other apps or
allow the user to access with a computer.
In order to read and write files there, you'll need this permissions on your manifest file:
<manifest [...]>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
WRITE_EXTERNAL_STORAGE is API Level 4, but 'starting API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir()' - catch being that files that your application creates there are considered private to the application.
How can i check Micro SDCARD is present in device and how i can access that to write some files.
You can connect your device to the computer and mount your micro sd card.
Then you can go ahead and transfer the files like you do for any normal USB drive.
There are several StackOverflow postings on these issues.
You want to use Environment.getExternalStorageDirectory() to get the path to the SD Card. You can then do standard Java File I/O.
Android how to use Environment.getExternalStorageDirectory()
You must use the following permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Permission to write to the SD card