Deleting Files From External Storage in Android 5.0+ - android

Before Android 4.4, we could easily delete files from any sd card directories. After 4.4 this is removed, even with WRITE_EXTERNAL_STORAGE permission you couldn't do it (except with some hacks with ContentProviders). But I heard after 5.0 this ability is re-added (don't know if it's wrong). I tried File.delete() method but it's no different than 4.4 for me. Is there any way of deleting a file from sdcard in Android 5.0+ ?

You can request permissions from the user to the sdcard through SAF:
How to use the new SD card access API presented for Android 5.0 (Lollipop)?

I was also facing the same problem in my Android version 5.0 but now I have no worries regarding this as I found the solution of this problem. Just do as I said: go to playstore and download "X-plore File manager" and delete whatever you want from your external storage as well as internal storage.

Related

Can't I access portable/external SD in Android 7.1.2? It was working on Android 4.4

I have commercial tablets running on Android 4.4. I made a simple app with some buttons, video's and txt files. The video's and the txt files are stored on the external sd card so I can change the content without changing the app.
In my code I used (AIR SDK as3):
"file:///mnt/external-sd/folder/";
as reference to the folder. But on the newer commercial tablets Android 7.1.2. is running and this doesn't work anymore. I tried to change the filepath in 100 different ways based on what I found online what this could be.. It is making me nuts.
I have set the write and read permission, just to be sure, in the manifest (I didn't needed them in the older version). But nothing changed...
I think something else might be the problem? Am I missing something?

Android | obtain the sdcard (removable) path

I've tried a lot of method and I've read a lot of posts, without find a valid soluction.
Summing:
I try to use this https://github.com/18446744073709551615/android-file-chooser-dialog to choose a directory and use it. This code work perfectly with internal storage.
With this code: System.getenv("SECONDARY_STORAGE"), on my Asus TF201 with JellyBean and with Lollipop, I have /storage/sdcard1 and I can use this with previus code and it works.
Now, the problem.
I've tried with this (and other similar solution): How can I get external SD card path for Android 4.0+? but on LG G3 with Android 6.0 I've /mnt/media_rw/9014-4EF8 and doesn't work with previous code.
How can I obtain the external sd path? I need only to read in sd. Or can I use /mnt/media_rw/9014-4EF8?

Erase app external files on uninstall Android 2.3

I've use the Android method Context.getExternalFilesDir() and save some data from my app on a external file. So far, when I uninstall the app on an Android 4.4, this external files are being erased as well, but this is not the case for Android 2.3.
I was wondering if this is possible and if so, how would I achieve this kind of behavior in Android 2.3.
Regards
Jose
4.4 changed the behavior for the SD card. Prior to that, data on the SD card had no protections, any app could read or write anywhere. Afterwards, files and directories were protected so only the app that created them could read or write them. Because 2.3 doesn't track who owns the files, it can't delete them when uninstalling. The closest thing you could do is delete the files yourself on a regular basis.

Why i can't copy from asset folder in "Lg g2, Android 4.4.2" well as in other mobiles?

For months the code provided in this answer was ok for me (Android - Copy assets to internal storage) to copy pdf files.....until yesterday a client report me that on his "Lg g2, with Android 4.4.2" this does'nt work....the app does'nt crash but the alert tell him "the document path is not valid"....
maybe because the mobile hasn't an external storage?
i'm not sure if the file copied well and the problem is opening the pdf file....but in others phone works ok...
EDIT 1:
Ok, I still dont knonw the reason of that on his LG G2 but, i solved this by checking if the external storage is available and writable, and if not, i copied the files into /data/data/package.name/ and then i used a content provider to read/access it....
I edited just in case this is helpfull for others
EDIT 2 (may 5th): well, the same client with the LG G2 told me that he actually has a sdcard...Reading this post (Howto avoid the "EACCES permission denied" ON SDCARD with KITKAT 4.4.2 Version. New policy from google) i believe is all because 4.4.2...so frustating...
So, my final solution was, check if the external storage is available, writable and if android API version < 19, and if not, i copied the files into /data/data/package.name/ and then i used a content provider to read/access it....like before ;)
Thx again
Possibly because 4.4 doesn't allow you to arbitrarily write to the SD card.

Where is my sdcard location on 4.4.2?

On Android 4.4.2 Environment.getExternalStorageDirectory().getPath() returns /storage/emulated/0 but this path does not exist on my Nexus5 Android 4.4.2. Environment.getExternalStorageDirectory().getPath() worked up until Android 4.4.2.
How can I get the /sdcard path on Android 4.4.2?
This path does not exist on my Nexus5 Android 4.4.2.
Yes, it does, for your process at runtime.
For example, this sample project downloads a file to Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS). If you log the location at runtime, when running it on a Nexus 5, it is reported as /storage/emulated/0/Download. And the download succeeds.
If you are looking for /storage/emulated/0 via DDMS or adb shell, you will not find it. For those tools, default external storage is /mnt/shell/emulated/0. Hence, the downloaded file from the above sample appears in the /mnt/shell/emulated/0/Download directory.
AFAIK, the difference is tied to providing separate external storage to secondary accounts.
The Storage Options documentation says to use Environment.getExternalStorageDirectory() (as you are already correctly using). This function is available on all versions of Android.
Are you seeing it return a path that isn't actually available on a 4.2 device?
Please note (from Environment.getExternalStorageDirectory()):
Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String).
Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.
Starting in KITKAT, if your application only needs to store internal data, consider using getExternalFilesDir(String) or getExternalCacheDir(), which require no permissions to read or write.
Sometimes /storage/emulated/0 can be written to, but reads fail... so tests for "writability" are not sufficient. This is such an annoying problem, I have come up with an equally annoying but effective solution.
Hardcode "/mnt/sdcard" Yea, I said it.
Looks like someone else said it first ... storing android application data on SD Card
More joy... http://forums.bignerdranch.com/viewtopic.php?f=414&t=7407

Categories

Resources