Diff between getExternalCacheDir vs getExternalFilesDir in android - android

I read the docs but I can't seem to figure out what the difference is. Is it something subtle that does not really matter? I want to create a cache of ten files. So when I went to use getExternalFilesDir, I noticed there is a getExternalCacheDir. So now I am confuse as to which one I should use (the name does not always tell the whole story). So can anyone put the difference in layman terms for me? thanks.

The ExternalFilesDir is not a cache, it will save your files, and they will remain there until the app is removed or the files manually whereas the cachedir will empty when needed

I'm a newbie to Android but would like to help you with the below link. The below article explores all possible ways / methods to use under different scenarios. It helped me in improving my understanding and hope the same with you and others as well
http://www.grokkingandroid.com/how-to-correctly-store-app-specific-files-in-android/

Related

How to get path of external storage android?

I would like to get external root path. I got it like that:
root = File(Environment.getExternalStorageDirectory().absolutePath)
but after release android 10 it has become deprecated. I know that you will say that here I could find a lot of solutions but I didn't manage to solve my problem :( The most popular solution is that:
ContextCompat.getExternalFilesDir(String)
but this solution didn't help me. I also saw these questions: question_1,question_2 and documentation. The one solution which I see is to write smth like that: /storage/emulated/0/ and maybe it will do what I want. But maybe you have better solution for this task?

Reading ICO file in android along with all sub-images

I have found Is there a way to decode a .ICO file to a resolution bigger than 16x16? from 2 years ago and the best suggestion was to use image4j. Unfortunately it does not work under android in particular (also), because the classes "IndexColorModel", "BufferedImage" and "WritableRaster" are not available.
While working around "BufferedImage" by replacing it with "Bitmap" may perhaps work and not using "WritableRaster", but instead setting individual (or a group of) pixels using setPixel may work as well, I cannot manage to replace "IndexColorModel", because I cannot wrap my head around it.
I am currently downloading a favicon from a website, which stores usually more than one image inside of it. The images are of different size. I read up on the structure of ICO files and analyzed image4j as much as I could. Yet I have troubles refactoring the various classes to not use AWT.
BitmapFactory is able to load ICO files; unfortunately it only loads the first image (this is my guess at least) and thus does not let me decide which image to load (let alone load them all and let me chose).
Does anyone know if anything changed from 2 years ago and/or would anyone be willing to help me refactor e.g. BMPDecoder from image4j? Or is there perhaps a totally different, easier approach to it?
I have created a library based on image4j that will allow reading ICO files into a List of Bitmap-objects. In contrast to image4j ico4a does not use any AWT-classes, but instead only makes use of Bitmap / Bitmap.createBitmap.
See https://github.com/divStar/ico4a .
While the library's performance might not be the best as it uses a Bitmap-object's setPixel method in a loop, it gets the work done and it's good enough for me.
In comparison to image4j my library (ico4a) only decodes/reads files. While saving ICO files could be done probably relatively easy, I have not done so since I do not need it myself.
If you have further questions or issues with the library, post them on gitHub and I will see if I can help.

Watching changes to a file in Android

I have a file on the Android SD card and would like to monitor any changes that happen to it through any external applications via code. Is there a way of doing that? Something like
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
http://developer.android.com/reference/android/os/FileObserver.html
has you tried FileObserver?
I don't think the system provides any sort of a callback type interface.
If you want that information I think you'd have to be check the contents of the file every so often and compare it to what you found last time. If it isn't equal then you know the file was touched by something else.
Surely this will end up being somewhat inefficient in terms of battery though I would imagine. If you must go this route I suggest as long of a duration in between checks as possible.
EDIT: I stand corrected. FileObserver looks like it'd be what you want. I've never run accross that one before. Props to #Grey.

Storing data in pre-defined XML files

I'm trying to figure out how to store data in pre-defined xml files (the files should be part of the project).
I've tried to several methods but non worked.
Instead of asking what is wrong with what I'm doing, I rather ask what is a good known working method to do that.
Thanks.
There are plenty of tutorials about parsing xml. Try those by IBM. Just place your xml in/assets and parse it. But I believe newest api has even more convinient way to do it. Refer to dev guide. As fo xml itself just follow common standarts.
http://www.ibm.com/developerworks/opensource/library/x-android/

determining if resources such as those in strings.xml are no longer used

I have done some significant re-coding on one of my Android programs and now I am unsure if certain xml strings are used anymore. In addition I have a few translations which makes the task even more difficult. Is there a tool to test this? This would be useful for drawables also.
I am using the eclipse plugin.
This question has been discussed in the irc channel before. There is no tool to test it, but I agree it would be useful. Note that resources can be referenced in xml, but they can also be referenced from code. Furthermore, resources can also be looked up by their identifier, and such lookup could be determined by runtime.
So actually you cannot determine 100% whether a resource is used or not anymore, but you can probably determine which resources are referenced in a static way (in xml or code). Depending on your code/app which you know best yourself, such approach might be sufficient in many cases.
The approach would be to write a tool that parses xml and java source files and also take the import statements into consideration. With that information you should be able to determine which resources you can get rid of.
The easiest way is to remove them all, attempt to compile, and re-add those the compiler says are lacking. It's a little tiresome, but it's certainly tractable.
Note, as Mathias already pointed out, that it's technically possible to access resources by name with a string at runtime, and the way I suggest here would remove such resources though they are, in fact, needed. However, this pattern should be really rarely seen in any application, and if you are the one who wrote it, you already know if/where you do such treatment.
Use grep to extract a list of resources to a file by way of sort
Use recursive grep through sort and uniq to create a list of those mentioned in any source file (make a copy of project without unused files or dispatch grep on a list of used ones, of course commented out code will be an issue)
Use diff on the two lists

Categories

Resources