I'm surprised when delete folder from gallery and getting that folder by programatically it's returning isExists() = true.
if(File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), directory).exists()) {
return true
}
Note: However it's happening mostly customised devices of android, is there any way to find directory is exists or not?
When you call File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), directory) you are actually creating folder and then you are checking for exist of that file, so you were always get true. Lookout documentation for constructor of File(File parent, String child).
Creates a new instance from a parent abstract
better to use concet string and get file like File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+ directory) to check exist or not.
Related
I have a DocumentsProvider that simply republishes a directory from an external file storage. Everything works well, however when deleting a file from the Android Files application, the directory does not get refreshed after the file is deleted. I'm probably supposed to call contentResolver.notifyChange, but I'm struggling to find the proper content uri.
#Throws(FileNotFoundException::class)
override fun deleteDocument(documentId: String) {
val file = getFileForDocId(documentId)
if (!file.delete()) {
throw FileNotFoundException("Failed to delete document with id $documentId")
}
// Notify parent about directory change
file.parentFile?.let { parent ->
val parentDocId = getDocIdForFile(parent)
val updatedUri = DocumentsContract.buildChildDocumentsUri(
BuildConfig.FILES_AUTHORITY, parentDocId)
context!!.contentResolver.notifyChange(updatedUri, null)
}
}
I tried many combinations with the Uri, but it still doesn't work. I even tried to make all the identifiers in URL lowercase, no luck. The only thing that seemed to have at least some effect was trying to refresh the root - it forced queryRoots.
Deleting the file on the Google Drive seems to trigger the proper refresh, so there is probably some way.
Ultimately this is the answer: https://stackoverflow.com/a/27583807/149901
When returning a Cursor from the query methods, the setNotificationUri method must be used with the proper uri. Then the notifyChange works correctly.
If I call File.delete() are the effects on the underlying file system immediately visible? Can I write to the same file name in the same process/thread after without worrying about bad things happening? If not, is there a way to sync the underlying file system with just a File object?
File.delete() return a boolean telling you if the file has been correctly deleted.
So you could write something like :
if(yourFile.delete()) {
//keep doing what you want. You are now sure file has been deleted !
}
Also, before writing a new file, you could check if a file with the same name already exists.
From Oracle documentation :
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
Oracle source
Also there is a SO thread that might help you
Naturally I thought to delete a file means to remove it from existence. So when I do
File file = new File(absPath);
....//add content
file.delete();
I expect that no further operation can be executed on file or it would throw an exception. But how come I can still add content to the file such as shown here Android saving Bitmap to SD card. So how do I delete a file so that it is completely gone? So that when someone go look through file manager, the file is no longer there? I am not in a position to test this now, so I was hoping for authoritative reference.
how come I can still add content to the file such as shown here Android saving Bitmap to SD card.
That code creates a new file after deleting the old one.
So how do I delete a file so that it is completely gone? So that when someone go look through file manager, the file is no longer there?
Call delete() on a File object that points to the file. Then, do not use that same File object to write to the file again, thereby creating a new file, as the code that you link to does.
I'm working on an app that works with database and offers option to create backup on external storage. App gets the backup directory from preferences, but since it's not set when I run app for the first time, I use a default path instead, simply by getting path to external storage and creating a directory there.
For all this, I use global string value which keeps the backup path. But here's my problem - when I run my app for the first time, I declare the default path in onCreate() method. When I try to write this path to log, it works. But then I have onClick() method, where I listen to click on button, which calls dialog with export/import options. When I try to write the backup path to log from here, the string is empty.
Variable itself shouldn't be the issue, because here's the interesting thing - when I go to preferences and set the backup path from there (which is then saved to string variable just the same way as it was before), I can suddenly get the variable from the onClick method and everything works fine.
Just for the record, I tried setting different path and I've always been choosing same path in preferences as I used manually in the onCreate method.
Any ideas what could be the issue? I can write the whole condition for backup path used in onCreate also into the onClick method and that would probably solve this issue, but I would like to avoid duplicite code and it's also really bugging me.
EDIT:
Here's code of the condition I use in the onCreate method.
location = PreferenceManager.getDefaultSharedPreferences(this);
if(location.getString("backup_location", "").equals("")) {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
File file = new File(Environment.getExternalStorageDirectory().toString() + "/simpledbmanager_backup");
if(!file.exists()) {
file.mkdirs();
}
this.backup = file.getAbsolutePath().toString();
}
}
else {
this.backup = location.getString("backup_location", "");
}
In my app I have a ListView which I populate with names of files and folders in a directory for my file browser.
I have overrid the getView method so that I can manually set a different icon in each row of the ListView depending on the child's file type, or if it's a directory or not.
Now if I were to check if a path on the sdcard is a directory I'd simply do:
if(new File("/sdcard/folder/path").isDirectory()){
icon.setImageResource(R.drawable.foldericon);
}
But when dealing with FTPFile's that method doesn't seem to work.
Does anybody know how I can achieve the same outcome as the code above but with an FTPFile?