Download and save static images in phone - android

In my application when i query a http URL i get list of object, some times these object needs to be rendered on listview and sometimes in my custom view.
Each of the object which i receive from server has image tag, most of the time images are always constant i.e same image URL only.
I just want to download the images once, & also my app should have to download once in a while when the images got changed in server. What is the best way?
I know like, we need to perform operation in thread, but not getting the exact idea of how to do?. how to check for change of image & efficient way of storing them. Any hint

For storage, I recommend Internal Storage
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
And for downloading
http://developer.android.com/reference/android/os/AsyncTask.html
You can create a AsyncTask where you pass a ImageView, where you want to show the img, with the url as a tag. And check in the internal storage if you have that img already saved if not just download it.

Related

retrieve images from firebase storage to recyclerview without database

Hi guys I am making a wallpaper android app.I have stored all my images in firebase storage. Now I have a recyclerview and want to retrieve each of the images into that recyclerView by getting list url of the folders.Is there any way to do that without using the database.I have pasted the json file into my app.
In order to download a image/images from Firebase Storage, you first need to have the corresponding url/urls. To download an image, it requires four steps:
Upload the image to Firebase Storage.
Save the corresponding URL to Cloud Firestore/Firebase Database database while uploading.
Attach a listener on the folder you have saved the image.
Display the image.
So there is no way in which you can download an image without knowing the URL. You cannot get the image list URL directly from Firebase Storage.
First at all you need to have corresponding URLs of your image data stored in Firebase, as mention Alex Mamo. You can build list of this urls during upload or copy them from Firebase Console. Storage API doesn't provide any way how to retreive list of stored files.
Also, you can find Download URL in Image detail/properties. Then you can insert urls into JSON file (or another file, service, firebase storage, whatever) and load them in app.
But be careful, this is not a good idea because Firebase Storage (especially spark plan has limits) allow you download 1GB/day and only 50k/day (download/upload) operations. That will be wasted pretty soon if you don't optimise your data and number of reading operations.
Tip: When you select pay as you go most expensive is GB Downloaded, so you absolutely should store at least 1 thumbnail which will be shown in your recycler view.

Firebase+glide, caching strategy

I'm using Glide and Firebase for loading and cashing images. Usually, I use Signature with image created time then determine cache time. But in Firebase I can get created time only using second request getMetadata(). How do I make caching correctly when i change one image to another with same name in my storage? Should I use getMetadata() or there are other ways?
Glide.with(getContext())
.using(new FirebaseImageLoader())
.load(storageReference.child(item.getImageUrl()))
.placeholder(R.drawable.category_image_not_found)
.signature(???)
.into(image);
I was stuck in a similar situation for as long as I can remember! Using getMetadata() is slow, and causes delays. What I figured was that to keep my image up to date, there was no other option but to incorporate the Realtime Database. This can be done in one of the following ways :-
First :
Whenever the image at the particular storage location is edited, you update the timestamp for that image's node in your realtime database. And when you want to display the image, just download its timestamp and write a Glide.signature(timestamp) method similar to what you mentioned.
Second :
Just obtain the download URL of the image whenever you upload/edit it, and save the URL in the realtime database. In this way, whenever the image is updated, a different URL is saved to the same location. This guarantees that your cache does not show outdated images (changing URL of source is the advocated method to invalidate caches for Glide).
I understand that there could be overhead involved with retrieving data from the realtime database first and then downloading the image. However, that's the only way to go when using Glide + Firebase. Plus, enabling persistence and other realtime database quirks can make it seamlessly fast!

Android : How to get large data above 20mb from server to android

In my android application when a user login from a new device i want to download all his data from the server database that may be more than 20mb in size.This data include bitmap images that are converted to string.When a user uploads a image what i done so far is i just convert this image to bitmap and then convert it to string and then save this to database,after that this data will save to server database by syncing.If the same user login from a new device i need to take all those data from server with a webservice.Right now i am using resttemplate to load this data, but the problem is when there is more than 20mb of data that mainly contains some image data the webservice may take more time based on the image size.Is there any better way to deal with images??
You can use aws s3. The images can be saved in the s3 buckets, which provides us with the URL. While loading all the details in the app instead of loading the whole image, only the s3 URLs needed to be send. The images can be downloaded later while loading them into the imageviews using libraries like Picasso or glide.
Do not use bitmap images (what ever you mean by it) but jpg's. Dont convert them to base64 as the amount of data will increase by 30%. Dont save them in a database on your server but on the server file system as normal files.
Then just give twenly urls for twenty files to the Android client and let the Android client decide when and how to download the images from url.
Alternatively you can let your base64 encoded images in the database. Just send twenty ids or file names (for twenty files) to the Android client. Then let the Android client decide when it wants to download an image using an url with parameter id.

How to enable offline reading in my application?

Context
I have a news activity which contains a ListView . Each item in the list contains some text and one image. Now every time the user opens the activity, the application makes a request to the server for getting data. Unfortunately if the user doesn't have an active internet connection, I simply put a Toast saying "Please check your internet connection"
Requirements
I would like to show news article from the last session if the user doesn't have an active internet connection. I define session as the time when user launches the activity and exits it by pressing back button.
Libraries used
I am using Retrofit Library for making Http Requests and Picasso for loading images.
What I have tried so far
While exploring Retrofit, I found out that retrofit by default caches the response for some time. So without any internet connection also I am able to get the data. In Picasso also, I found that we can save the image to any location on the device.
How to go for its Implementation ?
Since it will be a big change in my application, I wanted to know what is the best way to enable offline reading using the above two libraries? How do other applications manage to do so? Also if I can get some references or some blogs regarding this implementation, then it would be great.
Use the internal storage of the application.
Parse the image to a byteArray and the text to a single String so you can easily convert this also to a byteArray.
Name the files so you can easily retrieve them and link them back together.
You can storage the last updated data on local, with SharedPreferences or json file.
Instead of show toast, you can load the last updated data from local.
Picasso is a good choice for images, it saves images locally and reuse it automatically later.
For your items, I suggest to use a small local DataBase : in Android, we use SQLiteDataBase. here is a small tutorial : http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
So, I suggest you this pattern
user opens activity
system retrieves data from database
in parallel, system starts to download the new items.
When new items have been downloaded, you should notice user like 9gag or Facebook apps do.

Most efficient way of downloading large amounts of data to android?

I have an external database on Azure that holds large amounts of information. My app needs to be able to synchronize a selection of this data and store it locally on the app's database. I currently use web services to make calls to the database. When the app starts for the first time, it will have a large amount of data that would need to be downloaded. What's the most efficient way of downloading a large number of rows from the external database? I was thinking an XML file might be the best way, but I'm not sure. There could be thousands of rows that need downloading so I'm not sure which method would be the most appropriate.
There isn't 300 ways to download data, so you'll basically have to call your API and get the data. For obvious performance reasons, I would avoid XML and prefere JSON instead. So once you've got your JSON file, you parse it and put it in the locale database. I also would suggest you to use service as it won't be interupt, and don't forget to warn the user that you gonna download massive data ;)
If you wan't to minimize the amount of data downloaded, you can store a JSON file for example in the assets that will contains all the things "static" that won't change online.
I would offload the work to some sort of service. Services have the benefit of running in the background without interacting with the application. You would still need to create another thread to do the work. When the download is finished, it will simply destroy itself.
Data should be downloaded anyway whatever method you use, maybe you may prepare the initial data as XML file, download it the first time and store the data in the database showing a progress bar to the user

Categories

Resources