Downloading images from url and saving them on disk with Picasso - java

My app is downloading images with Picasso like this:
Picasso.with(getBaseContext()).load("https://www.sestavsisvujsvet.cz/files/magnetky/"+id+".png").placeholder(R.drawable.magnetka_placeholder).error(R.drawable.magnetka_placeholder).into(obrazek);
and it works well, but I need to save images to disk, because Picasso's caching mechanism isn't enough (I have up to 870 images which will be frequently seen by the user).
Is there any way to download the image, store it and load it from disk later?
I tried to find some solution to this, but I wasn't able to find anything useful.
Maybe it would be better to download them without Picasso, but I am not sure if it's possible.
I don't mind replacing Picasso completely and using another library, as long as it's capable of saving the images.
Thanks!

Please have a look at this for downloading image.
There are some changes in getting android storage for that please have a look here
After downloading images you need to manage them locally, use sqlite for keeping track of your files.
Basically you need to download images, and after success, save entry in Database which will include fields like location, file_name, create_on, etc. Now you can get images from database
Hope this will help!

Related

How to create image getting URL from JSON

I have an app that retrieves data from json, including the URL of the image I want to display in my app.
How exactly do I create an image with that URL?
I've been searching the web and found some third-party libraries (Glide & Picasso) but I was hoping there was a way to do it without those libraries.
It is strongly recommended third-party libraries (I prefer Picasso). It is more that just downloading the image. Functionalities like local storage to avoid unnecessary data transfer are too good to ignore, also complex enough to create from scratch...
But if you still want to do it manually try this:
URL url = new URL("www.yourimagepathgoeshere.com");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
If you also need JSON parser, try JSONSimple.
You can use it by downloading the image from its url and then making Bitmap of the saved image path and then setting it to your imageview using:-
imageView.setImageBitmap(yourBitmap);
But, i will prefer you to use the 3rd party libraries instead for this
purpose as they manages everything efficiently from downloading of
images in multiple threads to Bitmap Handling for memory efficient
implementation.
Easy way is use picasso or glide libraries.
But u can try this Accepted Answer link

Speed optimization of Website with a lot of images

I am currently working on a website which involves a lot of images. The problem is all the images are uploaded by the user so I can't do anything to alter the images. The website runs quiet ok on local system but the speed drops too much on the server,it becomes too slow
I'd suggest you to use Timthumb. It creates a thumbnail by generating a URL on the fly and uses very minimal disk space.
If the users of your website are uploading the images, then I presume (there must be) an upload script. Inside of that script or directly after its execution you could compress or rescale the image to size needed on the website, shortening loading time. There is a PHP image processing library called ImageMagick here:
http://php.net/manual/en/book.imagick.php
There is the PHP GD image processing library here:
http://php.net/manual/en/book.image.php
I don't have much personal experience with them, but from my knowledge it looks like one will do the job. Off the top of my head, that's the best solution I can think of, and hopefully it works. There is not a lot you can change about your problem if you don't compress/scale the images, and these are probably your best options. Wish you the best.

Android GTFS app

I'm trying to work on an app which uses GTFS. This may seems like a stupid question but I couldn't find any answer to it.
The GTFS for Israel, a rather small country with not so many buses infrastructure, is around 120 MB zipped file.
Right now the only possible way I could think of for getting it working is to download the file, but downloading 120 MB using the phone could take quite a long time. Sure you can do this only once and save it in a database on the phone, but it still requires downloading 120 MB.
Since it is zipped, I can't unzip it over the server and than just get the txt files..
So basically I'm asking, How can I get the information to the phone, without downloading the zipped file?
I've seen and used apps which uses that same GTFS file, and they load up really fast, even on the first load..
I hope you understand my issue, not sure how to explain it better.
Thanks!
P.s I would make an iPhone app too, and it's the same issue, hence the iPhone tag
One approach might be to preprocess the GTFS data during your app development. You could load it into a SQLite database, and use Core Data to get the data you need out of the file at runtime. This also gives you an opportunity to include only the data that you actually need for your app - it doesn't make sense to ask users to download extra data that they won't need.
Use protocol binary format (pbf) formely google and now open source. It is compact and very fast searchable, so no need to decompress it on a device and load it into a database on that device because pbf acts as a database. Just include pbf library in your code to query it. Of course you have to compress it once before distributing the data online.

Adding Many Saved Images in Android App

The application I'm trying to build will have a lot of images displayed (in ImageViews), and I'm not fetching them from a server/online service as it will need to be used offline. I know I can just dump them in the res/drawable directories, but I was wondering if there's any way to optimize this. Is there a way to somehow compress these images (besides making them smaller, they're already as small as I need) or use some other sort of android tool to better store them locally on the device?
I could just be overlooking a well used feature, and if so, it'd be great if someone could point me to that.
Edit: If I were to compress the images somehow, I would need to decompress at runtime or something, and that would take another thread/loading time. I'm not sure how to do that either, so I'm just brainstorming various ways, and I thought someone here would've come across this at some point.
If you haven't already, this is a good read - http://developer.android.com/guide/practices/ui_guidelines/icon_design.html#design-tips
When saving image assets, remove unnecessary metadata
Although the Android SDK tools will automatically compress PNGs when
packaging application resources into the application binary, a good
practice is to remove unnecessary headers and metadata from your PNG
assets. Tools such as OptiPNG or Pngcrush can ensure that this
metadata is removed and that your image asset file sizes are
optimized.
Outside of all other compression logic the above would be the place to start. Also when you say "optimize" - do you mean optimize the way images/drawables are loaded in your app or just the amount of space (on disk) the app will consume?

Where to save pictures on Android?

My application uses quite a lot of pictures that are downloaded from the internet and cached locally on the Android phone. I am wondering, what is the correct way to save those pictures. There are several ways I see, that are not fully satisfying.
Save them on SD Card in a public folder
Uses up space that wont be freed on uninstall
User can see pics in Gallery
Needs a folder on the sdcard root (you can actually see it while browsing your phone)
Save them on SD Card in a non-public folder
Uses up space that wont be freed on uninstall
Secretly uses space on the SD Card
Save them inside the application
Blows up application size far too much
What is the correct way of locally saving the images of my application to not distract the user and leave no garbage anywhere?
Your best solution is to use:
context.getCacheDir()
This directory is private to the application and will be deleted on uninstall, furthermore the system can delete from this directory for you if the device is running short of space.
Note though that the docs say:
you should not rely on the system
deleting these files for you; you
should always have a reasonable
maximum, such as 1 MB, for the amount
of space you consume with cache files,
and prune those files when exceeding
that space
If you need a lot of space and would rather use the SD card you can call
getExternalCacheDir()
instead. These will also get removed on uninstall, but the system does not monitor the space available in external storage, so won't automatically delete these files if low on space. If using this option you should also check that external storage is available with
Environment.getExternalStorageState()
before attempting to write to it.
You can hide images from the MediaScanner if you put it in a hidden dir (i.e., with a dot prefixed) such as /sdcard/.donotscan/.
Update: As romainguy mentions on twitter this also works if you put a file named .nomedia into the dir.
I think the best way is to use the database.
It does not blow up the application and memory.
The related database is deleted once the application is uninstalled.
Nobody can reach to this files besides your application.
Update: But; If you want to cache only the data, there is a cache manager defined in webkit. CacheManager
I didn't use the package before but the methods seem straight forward to use:
static boolean cacheDisabled()
static boolean endCacheTransaction()
static CacheManager.CacheResult getCacheFile(String url, Map<String, String> headers)
static File getCacheFileBaseDir()
static void saveCacheFile(String url, CacheManager.CacheResult cacheRet)
static boolean startCacheTransaction()
and you can find the usage at Google Gears code
I hope this helps.
If you you don't want to use the CacheManager then use a database or a local (non-SD) file (local files get deleted on a complete uninstall) and register to receive the 'ACTION_DEVICE_STORAGE_LOW' and 'ACTION_DEVICE_STORAGE_OK' broadcast actions. Then you'll know when your application is taking up too much space according to the device and when you need to start deleting pictures. Your application size will still grow, but you will be able to manage the growth and shrinkage.
Just a tip - if you save them on the SD Card, they will be scanned by the MediaScanner and will appear in the users's Gallery (Photos), which you probably don't want. So you probably want to store them on the phone, or somehow tell MediaScanner not to scan them (apparently impossible after a quick Google search.)
Probably best to store a select few in your application's private directory, which will be removed when your application is uninstalled.

Categories