Bitmap loading from url using Async tasks and handling concurrency - android

I used the same code as shown in the linkMultithreading For performance, but I cant see the image on the screen.

Dear Its 2012 try using latest functions for downloading image or something else
it has following:
1) progress bar
2) provide url in download manager class
3) register BroadcastReceiver to get downloaded data after it is downloaded.
Download Manager Class Example
Hope this is easiest one.

Believe me, you don't want to do it yourself manually if it's for UI purposes. Way too messy on Android, if you just want to display the picture to the user. Just use http://loopj.com/android-smart-image-view/ for those scenarios, it also includes caching and will help you with out-of-memory issues that Android is prone to.
When you get it working, make sure to read how SmartImageViews work internally, you can probably learn a few things.

Related

Idea to download images in android

I'm making an android app, here the images are getting from Cloud, is it good idea to download images and save it & use it further. Or download images every-time user uses the app, what idea you prefer is the best?
Because downloading images always is slow & its bad i know but at some point if the images are updated then how to get to know about it?
You should definitely cache your downloaded files!
Do it in your internal app directory where only you do have access to (or otherwise external storage, thats still ok).
Bandwidth and connections are always expensive and should kept low as much as possible.
So your user can see images fast even on a bad connection and your app doesn't waste his valuable bandwidth of a users data plan.
Maybe this could also help you:
https://github.com/novoda/ImageLoader
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
Make it easy on yourself and use something like Android Smart Image View. It takes care of loading and caching, and it's just about a drop-in replacement for Android's ImageView. Universal Image Loader is another alternative, more configurable, but not as quick to implement.
I used https://github.com/nostra13/Android-Universal-Image-Loader
but I think you not want only download and cache.
these no trick ,if you want check weather the image update or not, you can add metadata for image, just like md5 .
in html and browser, you can set expires header for a image:
enter link description here
but in android app, you control all yourself.
Downloading images and saving them is probably the best way to do it because you don't want to download the same images over and over. If the images are updated you can delete the older one and download the new ones. Just make sure you don't download/save a million images. Take a look at this library. It has a built-in cache on sdcard/external sd.
Downloading images from the net for display, with possible requirement of caching is a very common problem that many people have solved, you can try these solutions to see which fits you:
Ion (https://github.com/koush/ion) - very flexible and feature complete, plus it can download more than images but JSON, Strings, Files, and Java types as well. The part that I really like about this is that it can automatically cancel operations when the calling Activity finishes, so users don't waste time & bandwidth downloading images that will no longer be displayed
Universal Image Loader (https://github.com/nostra13/Android-Universal-Image-Loader) - equally capable for most use cases but for downloading/caching images only

The best way to download a big file in android?

I need to download a big file on my app (almost 2gb) and i was wondering what is the best way to do that and some libs to help me.
First I looked the Android Asynchronous Http Library but I didn't find examples showing how to publish the progress or start, pause download. Then I don't know if should I use this lib or just use a standart http commons.
Other problem is, should I use a service to download my file??
Could you guys give a hint, how to do that?
I've see you got a bunch of answer and none of them really answers it from a holistic point of view.
To download a 2k or 2gb file the actual code is the same using some type of connection handler and some type of input stream and output stream and usually wrapping the input stream with a buffered input stream. For that you can find infinite amount of java examples all around the web.
The trick here considering the Android platform is that because it's a lengthy operation, you shouldn't be doing it inside the activity life cycle at all.
Said that you have two options:
as you suggested you can create a Service, I would suggest you to use the IntentService
as it seems to be a perfect fit for this case. The IntentService automatically spans a new thread, through the intent you pass the URL as a String and let the service download using the streams, etc.
another method that probably work well, but I've never personally used, is to use the DownloadManager that is available since Gingerbread. It shouldn't be difficult to call getSystemService(Context.DOWNLOAD_SERVICE) and call dwManag.enqueue(dwRequest);
hope it helps.
If you're not targeting pre-Gingerbread devices, I would use DownloadManager as suggested as the third option in the answer you linked to. It takes care of downloading the file, displays the progress in the notification bar so that the user can see what's going on even after your app has gone into the background and you don't have to worry so much about what happens when the user goes into another app and android decides to kill your app to free memory. Also, it works with features like "only download files over wifi" that at least some android builds have.
I suggest you to use adm download manager. Downloads never fail even if there is no network and the speed is also best.

Android Best way to use AsyncTask

I've been looking at this guide:
http://developer.android.com/training/basics/network-ops/connecting.html
And was wondering what would be the best way to download multiple files. First I need to download a text file from a url to determine which files to download.
Should I have 2 separate ASyncTasks, one to download the file and then the other to download the remaining files? Otherwise my code which depends on the first file crashes since the Async task does not complete in time.
Also for the progress dialogue should I make a new one for each file or try to update the previous one?
Thanks
Orginally I was creating a new AsyncTask for each file to download.
In general, if you want the files to remain on the devices, and you're downloading multiple files based on the results of downloading one file, then you should
Use an IntentService to download and store/parse the first file.
Use an IntentService to read the results of the first download and then download the remaining files. If you need to, you can use a progress bar notification in the notification area. Meanwhile, the user can continue working in the app or even switch to another app and the download will continue.
An IntentService is immune to Activity lifecycle changes that might kill an AsyncTask.
Any time you download data, persist it somewhere. You can always check to see if the data is outdated. On the other hand, if there's no connectivity, users have the last "good" data.
To learn more about IntentService, see Running in a Background Service. The content provider in the sample app illustrates downloading "metadata" for other files. The sample also demonstrates how to check for connectivity before downloading.
There is no perfect answer to cover every situation possible.
If you are happy with one quick running AsyncTask, don't change anything.
If you are using API 9+, you could switch to the DownloadManager class and let it workout the particulars.
If you need references, Download a file with Android, and showing the progress in a ProgressDialog, provides examples for multiple ways to download a file with an active ProgressBar.

Asynchronous Image download freezes android emulator

After some googling, I selected various sources and started to use a separate thread to download images to make the UI responsive. It actually worked like a charm. But after a few minutes it would freeze the emulator. Initially I had assumed various reasons but finally I figured out that if this threading code is removed it works without freezing the emulator.
The code was adapted from another Stackoverflow question from the answer given by a certain Fedor. For the sake of simplicity I had removed the HashMap part and directly download the image each time a request comes from the list adapter. Also, I assumed that since the image is very small (< 1 KiB) it can actually be downloaded again rather than storing it in memory.
I am not sure if this is the right way to handle asynchronous image download, but any help in preventing the emulator freeze would be much appreciated. I can copy paste the code if needed.
Have a look at this url
about downloading images from remote server using asynchronous task and threadpool.

What's the best way to display a bunch of images from a web server in Android?

I need to load several thumbnails (the images on the server are full sized images, so I need to resize them in the app), and display them in a way that allows for the user to click on one (or more) and have some associated id be given back to the app for use in a larger context.
so, in pseudo code...
ImageObjArray ioa = loadImagesFromServer("http://server/listOfImages.php");// this returns
for(ImageObj io : ioa){
drawThumbnail(io); //io contains both a jpg and a reference id
}
//later when a user clicks on a thumbnail
clickHandler(){
passIdToBundle(this.refId);
}
So, I've never played with a webview before but I suspect it might be the best approach here, though I don't know how easy it is to send info back out of a webview to the larger app when the user clicks on a thumbnail. Or is there a more elegant approach?
All advice welcome.
Using a WebView would be the quickest way to implement this. Otherwise, you have to fetch each image and write them to the device in order to display them in a native Android widget. There are several ways to approach this, like: http://www.dreamincode.net/code/snippet4724.htm
You can use WebView.addJavascriptInterface to communicate with native code. A good example can be found in the WebViewDemo from the apps-for-android project on Google Code.
You might want to consider a framework like PhoneGap to help with the Java to JavaScript interface.

Categories

Resources