where on android to save files downloaded from server - java

I am downloading a number of images from the server. That happens once in a while (i.e. as needed). I need to know which is more efficient to the operation of my app: do I download the images to a HashMap of images or do I download them and save them as resources in the asset or drawable directory and then access them by id? Please do explain, at least a little, why whichever option is better.

Ideally you need to download all the images to sdcard and store them.
Access them through the file apis and load them using lazy loading to your image views.
If you are going to load all the images in the app resource folders, then it is going to eat up lot of memory. And keep in mind do not cache all the images on to the memory. I would suggest you to use, UniversalImageLoader from my experience. It gives you a bunch of options to play with.

Related

Java, Codename One: Exporting and -most importantly- importing files between users

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.
This requires 3 steps:
Saving the data: easily done in Storage implementing the
functions required by the Externalizable class;
Sharing the data: done (probably, right now it's impossible to check if the
result is correct because the missing step 3) with the sharing
methods offered by the framework, as soon as I understood I needed
to use as mimetype "application/octect-stream";
importing the downloaded data (shared by another user): this one I can't manage to
find a way to make it work. Loading the files from the app's Storage
is easy, but accessing to the folders out of the app's Storage is
something I can't manage to do.
I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task
Using the FileSystemStorage on Android, for example, I have access to
/storage/emulated/0
/storage/emulated/legacy
file:///system
The first two being related to the Storage of the app.
Acceding to file:/// I obtain a long list of folders, a partial list including
media
logs
sdcard
Removable
...
root
...
But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.
So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.
Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.
As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.
Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.
According to this blog post it should give access to, between the others, the Download folder (at least in Android).
I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.

Static images in webpage from Google App Engine

I'm porting an old Java project to GAE. It has some servlets, which generate html pages with static images in them. In the original project these images are stored on the filesystem next to the servlets.
I'm trying to use GCS in the first place, I've uploaded my files and gave permissions on public read. In this case I can reach the files with their public link, I can embed these links into the HTML output. But I have a feeling that this isn't the right solution. The load time seems quite slow, like the images don't "travel internally", and I have to provide permission for every single image.
So my question is, how to get an "internal" URL for a file located on GCS in your GAE application?
I've found some Java examples, but in my case I don't think I need the image object in the source, I just need an URL to pass it on to the HTML source.
As far as I know I could just simple deploy the images with the source as resources, but there are quite many of them.
If there are other soultions, like Datastore, I'm open for that too, but I thought GCS would be the easiest.
Google Cloud Storage is as fast an option for loading images as any other. A browser reads a link and asks the server (in this case GCS) to deliver an image. There is no "internal" URL that can work faster - the speed reflects the bandwidth/distance between GCS and the browser which asked for an image.
You can speed it up by using a CDN, where your image is stored on local servers throughout the world. It only makes sense if you serve content to a very large number of users, and it is a critical part of how fast a page loads.
Another way to speed up page load time is to use image sprites instead of images. This way you cut the number of requests from a browser to a server (i.e. GCS). If you images do not change frequently, and most pages need the same "collection" of images (i.e. not shown dynamically), this is a very good solution.

is this the best storage option for me?

I want to save downloaded images from a server. I want these images to be accessible only from within the application itself. I don't want the images accessible from anywhere else, i.e. someone can just delete/modify it like if it was on the SD card or from another different application. I'm thinking it would be best if I were to use internal storage, as it is private to my app.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
are there better options out there?
Internal storage is the best option if you want to keep your files private.
However, on most devices, internal storage is very limited and the users might uninstall your app if you use too much of it (images can be pretty big).
You should look into using the external storage to save images, and possibly encrypting them if you really want to keep them private.
As already mentioned by Raghav this would be a good option, and as said the internal storage is very limited (mostly on older devices), however if you are on a rooted device it will be possible to delete your files anyway. Take into account the limited storage, as people is also making "swap partitions" on their SD cards because internal storage is very limited.
This is is the simplest solution. The other typical solution is to store the image as a blob in the application's database. This will make it completely inaccessable outside of your application. You can do it using the method shown here:
How to store and retrieve a byte array (image data) to and from a SQLite database?
The draw back to this is the relatively small memory space you will be given for your DB (<60MB), so this only works if you have a small number of images.

File operations doesn't work sometimes

Something very strange is happenning in my app. I am creating 2 folders in the SD card if they not exist, and downloading some images from a URL if they not exist in the SD card.
Sometimes, when I run the application, the program checks if the folders exists and also the images, as they are exists, it continues and there's nothing wrong.
But often when I run the application, it alerts me that the folders not exists and that it didn't even managed to create those folders, and afterwards I'm getting a "File not found exception" when it tries to download the images who already exists in my SD card.
P.S: I tried it with 2 devices and the same happens, sometimes works, sometimes doesn't.
What could be the problem here?
Actually before accessing files from external storage you have to check whether External storage is present (Available) or not on device (It possible if device's sdcard is mounted on System). For this You have to check the sate of External Storage as Mounted or Not.
Look at this Link for more info..
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
Now solution:
If you find external storage is not available then you can store your files at Internal Storage on temporary basis (Then you can move these files to External Storage when it present).
So My link also help you in how to access Internal Storage in Android.

Android: assets alternative

I'm using assets as standard icons storage but now I have request that these icons should be updated for some cases. I know that I can't touch assets, so do you have any suggestions where to store them?? These files should be pre-installed and updated for some cases. From start I've been thinking about Internal Storage but now I have some doubts. What do you think???
So the solution to this problem was to put current available images in assets. App checks is there anything new every time user logs in, if yes places new images in /data/../images folder. When icons are required, I first check assets and then the storage. In new app releases I include the new icons in the assets folder.

Categories

Resources