Getting root directory contents - android

I have an app with data stored (by previous developer) in user-based directories: context.getDir(userLoginHash, Context.MODE_PRIVATE). And there is no users list - when user logs in app tries to load contents of his directory (or creates new one), when user logs out directory remains on the device.
And now I wan't to clean the data. So I need to get list of such folders, but didn't find method for that. It confused me a lot because task looks too obvious. Do I miss something? How can I do this?

So I need to get list of such folders, but didn't find method for that
There is nothing really built-in for that.
How can I do this?
Start with:
File motherOfAllUserDirectoriesAndOtherStuff = getDir("whatever", Context.MODE_PRIVATE).getParentFile();
You can iterate over all of the contents of motherOfAllUserDirectoriesAndOtherStuff using standard Java I/O (e.g., listFiles()). However, bear in mind that not everything in motherOfAllUserDirectoriesAndOtherStuff will be your user directories. SharedPreferences, databases, other files, etc. will also be in there. You will need to devise an algorithm that can determine whether a subdirectory of motherOfAllUserDirectoriesAndOtherStuff is one of your user directories or not, perhaps based on what a hash looks like.
If you can someday rewrite this, create your own root and put the user directories in there. IOW:
File userDir = new File(getDir("myAppRoot", Context.MODE_PRIVATE), userLoginHash);

Related

android: How to remember file passed via ACTION VIEW intent

I have an android app that is able to open a certain file type via a VIEW intent.
After a file is opened using my app for the first time, I would like the app to "remember" the file so that the user can choose to open it again from a list of "recent" files inside the app...
My question is: what is the best way to implement this kind of "remembering" - should I:
Automatically copy any files passed to my app into my app's own storage area, and then list "recent/old" files there?
Or, should I record a list of files that my app has been passed previously, and access them via the same path later if necessary? If that is recommended, is there any guarantee that I will be able to access them again later? (I guess not!)
Option 1. seems like more work and doubles the storage space needed for all files passed to my app, but will guarantee the files will be accessible in future. Option 2. is easy if the files are always readable by my app in future, and are not renamed/deleted for some reason - it seems there's no guarantee of that though...
If it helps, I expect most files passed to my app to come from "Downloads" via the user's browser, but some might come from email/other apps etc.
Thanks
I would go with Option #3: drop the proposed feature.
As DeeV pointed out in a now-deleted answer, Option #1 is not a great solution for a "recent files" list. It would be the right option for other verbs than "remember", such as "import".
Option #2 will not work much of the time. Your app needs to support the content scheme, in addition to (or even instead of) the file scheme. By default, you will only have rights to access the content at a content Uri until your process terminates (at best). You may be able to takePersistableUriPermission() to get durable access, but that will not work much of the time — it depends upon whether the other app is granting you such access. Hence, you might have a Uri that you can remember, but remembering will do you little good.

Kivy on Android : keep local storage files after app updates

I've got an Android app written in Kivy (Python), which stores local files that should survive an app update (adb install -r).
If the files are stored in a subdirectory of the current directory ("data/data/app_name/files"), I see that they are deleted after update.
However after some experiments I could "solve" this by storing the files in the "data/data/app_name/shared_prefs" directory, which seems to be persistent after updates. By the way, I didn't check but maybe the "data/data/app_name/databases" also is.
Is there a cleaner way of doing things ?
I need to test if I can create a new folder not called shared_prefs nor databases under "data/data/app_name", and if it is persistent.
(this seems kind of a hack because those directories have another dedicated purpose, even though my app is not using them for this dedicated purpose right now)
(NB: I don't want to keep the files outside the app private directory)
There is not a simple way (as in a build hook or similar) right now, but it's something we've specifically discussed in the last few days as the current situation has become a direct problem. I'm not sure what the resolution was, but there will probably be a change in python-for-android to fix it fairly soon.
If you want to keep up to date with this, ask on the kivy mailing list or irc. In particular, knapper_tech was making these changes.

How to copy cut files safely

Do you know any way for safe copy cut operation in Android?
I want to make a file manager. I can do them with FileReader/Writer, but as is not safe (if app crashes), I want another safe way to do this.
You should probably design a fail-safe order of operations. For example (just making this up, haven't validated it thoroughly):
Record in a persistent worklist file (/shared preference, etc) what you intend to do
Copy the file
Verify the copy is complete
Only then remove the original
Cleanup the record in the worklist
You should not do the work on the UI thread. You may want to consider doing it in a service to handle the case of huge files (or large numbers of files) which may take a while.
You may also want to consider if you can in some cases use the File.renameTo() method - this would require that the source and destination be on the same partition (ie, both on external storage). Also you would have to make sure that the destination directory exists.
Also put some thought into what you are going to do if the selected object is a directory rather than a file...

Creating a file on an Android device

I am new to Android development using eclipse, although not new to software development in general.
For my first real project, I am trying to modify the example SoftKeyboard that is supplied with the SDK. I want to modify one of the keys to act as a function key, when followed by a single letter key it will enter a canned string - performing a macro function.
So far so good. I have the key and graphics modified, and found where to respond. I would like to put the canned strings in an editable Properties file stored where the keyboard can find them.
That is where I'm having trouble. It seems that I can't to create and save a file. I don't know if it's read/write permission problem, whether the keyboard (it runs as a service) is not allowed to create a file, or my code is just plain wrong.
Can someone help me out – point me in the right direction?
Thank you very much.
Barry.
If these are canned files that come with the APK you install to the device and only need to read (not write), you can place them in the assets folder of your project. Then use the resource manager to load them:
Resources resources = getResources();
InputStream moduleSearchTemplateIn = resources.getAssets().open("file/name/here.properties");
If you want to read/write files on the SD card, you'll need to add a permission to your manifest. Though, for this purpose, I'd probably prefer a SQLite table.

Is it possible to make use of the Android resource resolver on a self-created folder in the file system?

I'd like the ability to "overwrite" the Android resources packaged within my apk by having the app periodically download a zipped file containing overrides that follow the same naming convention as the source does. For example, the zip might consist of the following paths:
res/values/strings.json
res/values-land/strings.json
My code would parse those files and produce a Map> that would map the string resource id to a folder->value pair (or something along these lines). At this point I'm really only concerned with strings and images (maybe arrays), but not layouts, etc.
To the point: Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state? I'm assuming this is difficult because the compiler breaks everything down to ids, but figured it was worth a shot. Any ideas?
Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state?
No. You are welcome to roll this yourself based on Configuration, DeviceMetrics, and kin. You will also need to create your own parsers for your own files, as Android's resource system only works with resources, not arbitrary files retrieved from arbitrary locations.
The expectation in Android is that if you want to update resources, you update the app, probably because there were also code changes as well. Admittedly, this approach has its limitations.

Categories

Resources