Version Android SDK 5.1
I do this Code.
String path = Environment.getExternalStorageDirectory().getPath();
String innerPath = "/xxxx/strage/Download/"
StringPath = path + innerPath;
File[] files = new File(targetPath).listFiles();
for(File f:files){
f.delete();
}
after execute this code, I checked Dir by "FileCommander.apk" I confirm deleted.
but when conect this Android to PC And Check this Directory, not deleted.
Please help me.
File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder"); // My Folder is Folder
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
if(String.valueOf(listFile[i].getAbsoluteFile()).equals("/storage/emulated/0/MyFolder/Adu.jpg")){//absolute path
File f=new File(String.valueOf(listFile[i].getAbsoluteFile()));
f.delete();
}
Log.d(myTag, "Response : "+listFile[i].getAbsolutePath());
}
}
This is unfortunately a known old Android bug, which google does not care about.
In short:
Sometimes depending on the way the file was created, it is not showing in the media device storage, when Android device is connected to the PC as MTP device. They should appear (or disappear in your case) after reboot, but there are known situations when reboot didn't help too.
String innerPath = "/xxxx/strage/Download/"
seems like strage is written wrong.
shouldnt it mean String innerPath = "/xxxx/storage/Download/" ?
Related
So in my react-native module I Want to find the already created files that my app has created in
/storage/emulated/0/Download/xx/xx
It work great when My app create those files in those directory and File.listFiles("/storage/emulated/0/Download/xx/xx") return all those files without any problem.
Now when I uninstall my app and install it again, listFiles cant find those old files anymore!!
But only find the files that I create a new.
Now for those of you who think this is about permission I already check it and have full access
status{"android.permission.WRITE_EXTERNAL_STORAGE":"granted","android.permission.READ_EXTERNAL_STORAGE":"granted"}
Here is the Java method that I have
#ReactMethod(isBlockingSynchronousMethod = true)
public String getAllAppDownloadedFiles(String folderPath) {
File folder = new File(folderPath);
String result = "";
if (!folder.exists())
return result;
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile())
result += file.getAbsolutePath() + (files.length - 1 > i ? "," : "");
}
return result;
}
I should point out that the problem only exist when I run the app on my mobile and not in emulator. I suspect it has to do with the version of API.
Any Idea how I could fix this?
Any app can create folder in external storage.
This folder can be placed at SD card or phone inner memory.
If i use this code, i can to get list of all my external storages.
File file2[] = getExternalFilesDirs(null);
it return:
/storage/emulated/0/Android/data/com.snailp4el.android.tatoeba/files
/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files
but if i try to get list of it path:
File f = new File("/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files");
File[] files = f.listFiles();
I get nothing.
in this case:
File f = new File("/storage/");
File[] files = f.listFiles();
i get:
/storage/8639-0FFD
/storage/emulated
/storage/self
That is OK. i finally get all storages in my phone.
Question is:
How to scan subfolders to find particular folders by name?
And is my approach right. I mean to scan "/storage"?
And if Yes will it work at all version?
and finally what is "/storage/self"?
I solved it this way.
May by it is not the best solution but it works fine for me.
public ArrayList getExternalFolderPath(String folderName){
ArrayList<String> arrayList = new ArrayList<>();
//get all storages in phone
File externalStorages[] = getExternalFilesDirs(null);
for (File es: externalStorages){
String exF = es.toString();
//cut out what don't need
exF = exF.split("/Android/data")[0];
File file = new File(exF);
File[] files = file.listFiles();
for (File f: files){
Log.i(TAG, "getAnkiExternalFolder()" + f.toString());
//add in array the path
if(f.toString().contains(folderName)){
arrayList.add(f.toString());
}
}
}
return arrayList;
}
You can get two directories(from both storage). The directory you need the last changed one.
file.lastModified()
I'm trying to save pictures on my Android phone and then restore them when it's needed.
So I use RxJava to save and read this files to Environment.DITECTORY_PICTURES folder. My problem is that I can write an image (my rx method calls onNext, but not onError, debug shows the correct path, settings testify that app data size grows), but can't read from there.
When I use my reading code with the correct path its throws FileNotFoundException. When I use this test code
try {
String path = Environment.DIRECTORY_PICTURES;
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
} catch (Exception e) {
e.printStackTrace();
}
it throws NPE saying that files array is null.
I have Read/Write external storage permissions granted. Tested on Android 5 and 6, so I guess there is nothing about Runtime permissions as well.
Is there something special about reading from Environment folders I don't know?
Thanks in advance for your help!
Environment.DIRECTORY_PICTURES is not a fulll qualified path to the file system directory, the one you looking for is get trough the class and using this as argument.
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Theres more kind of storages described at:
https://developer.android.com/reference/android/os/Environment.html
I went through the following link which says that the external folders will be deleted automatically during uninstallation of my app.
I am using the following code to create the folders and file:
private static String TEMP_FOLDER_PATH = Environment.getExternalStorageDirectory() + "/myAppFolder/";
My problem is that the folder myAppFolder is not getting deleted when I uninstall the app.
Am I going wrong anywhere?
Save it in your Apps Private Folder (/data/data/yourappPackege). This folder will be removed when uninstalling the App.
You can get your private Folder with the Method getFilesDir()
Other files can not be removed because your App does not "know" when it is being removed.
Hey the link says that If you use getExternalCacheDir(), then only folders auto deleted when uninstalling the app. So please correct your self. If you are using getExternalStorageDirectory , then you have to manually delete the folder by programming
to delete a folder you can use below code
String TEMP_FOLDER_PATH = Environment.getExternalStorageDirectory() + "/myAppFolder/";
File f1=new File(TEMP_FOLDER_PATH);
f1.delete();
No. The Android OS does not remove the SDCard files corresponding to one App when the App is uninstalled.
see this work for delete
public static boolean deleteDirectory(File path) {
if( path.exists() ) {
File[] files = path.listFiles();
if (files == null) {
return true;
}
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return( path.delete() );
}
I'm facing a problem in the following code. What I'm trying to do is delete a folder and all of it's contents. Sometimes it works and sometimes it doesn't.
boolean success = false;
String directory = Environment.getExternalStorageDirectory().toString();
directory += "/.SID/Downloads/DC0601";
File path = new File(directory);
File[] files;
try
{
files = path.listFiles();
if (files == null)
{
success = path.delete();
}
else
{
for (int i = 0; i < files.length; i ++)
{
File currentFile = files[i];
if (currentFile != null)
currentFile.delete();
}
success = path.delete();
}
}
catch (Exception e)
{
success = false;
Log.e("deleteData Exception: ", e.toString());
}
What's happening here is that in some cases, the directory 'DC0601' does exist and does contain files on the sdcard, but when this code runs, success is returned as false because 'files' is null. Why is it null?!!
I simply can't understand it. If I completely shut down the app and then run it, it can detect the files and the directory and can successfully delete them. Otherwise if I've been using the app for a while and then run the code, it thinks that directory and those files aren't there.
Has anyone faced similar issues with delete()?
I see you're grabbing the external storage directory, but I don't see you checking it's state.
What does Environment.getExternalStorageState() return?
Should you be using
Environment.getExternalStorageDirectory().getPath()
I've never actually used .toString().