The server is sending a list of urls of images when the application loads.
After parsing the url,The Application is Supposed to fetch that images from the server and display those images as MARQUEE on it's header as a BANNER.
On Clicking on that banner...a link is to be open(Say for example link of any WebSite).
Can Anybody tell me how to fetch this image from the url and Save it temporarily and Display them as Banner.
Regards.
I used this code for loding img form url
ImageView v_thumburl = (ImageView) rowView
.findViewById(R.id.v_thumb_url);
thumburl = temp.getString(temp.getColumnIndex("thumburl"));
Drawable drawable = LoadImageFromWebOperations(thumburl);
v_thumburl.setImageDrawable(drawable);
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
try this i hope it may help u
Use AsyncTask for downloading images from server (Store them in external storage).
After complete the download display those images in Gallery view as a
Banner. (Put Gallery view in banner).
As Android Gallery doesn't support Marquee, use animation for Gallery
view (Like Marquee).
For Loading Image from The Server you can use LasyList which will fetch the image from server and store it into SD Card.
SlideShow will be better than Marquee, so if you want SlideShow see this
And If you want marquee then what you can so is have and HorizontalListView
Add a public Method in HorizontalListView Class as shown below
public void getScrollWidth() {
return mMaxX;
}
public void getCurrentScrollX() {
return mNextX;
}
and For Marquee have a Thread and a Handler in your class like this.
new Thread(new Runnable() {
#Override
public void run() {
try {
handler.post(new Runnable() {
#Override
public void run() {
if((horizontalListView.getCurrentScrollX() + 50) < horizontalListView.getScrollWidth())
{
horizontalListView.scrollTo(horizontalListView.getCurrentScrollX() + 50);
}
else
{
horizontalListView.scrollTo(0);
}
}
});
Thread.sleep(1000);
} catch (Exception e) {
}
}
}).start();
Related
I use the following code do get an image from a regular image url:
try
{
url = new URL("http://example.com/test.jpg");
final Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
final ImageView imageView=(ImageView)findViewById(R.id.imageView2);
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
imageView.setImageBitmap(bmp);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
However, I want to download images that are displayed by html or php pages.
url = new URL("http://example.com/showimage.php");
If I use this, I get the following message:
SkImageDecoder::Factory returned null
How should I modify my code?
I recommend you to look at this library: http://jsoup.org/
This seems to be the solution when the page is redirected: http://blog.kosev.net/2011/01/follow-302-redirects-with.html
I am a learner and have been working on a home visitor app to get image from a URL of the visitor. Using the following code, which I found online, I was able to load image by adding an intent before the screen and adding a button on the screen saying "See Visitor Image" but now I want that my image should load as soon as the app launches. What changes could I make to do that? Thanks for your help.
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();
Editable ed = inputUrl.getText();
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
}
};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
inputUrl = ((EditText)findViewById(R.id.imageUrl));
inputUrl.setSingleLine();
inputUrl.setTextSize(11);
Button getImageButton = (Button)findViewById(R.id.getImageButton);
getImageButton.setOnClickListener(getImageBtnOnClick);
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
You can make use of SmartImageView, it is a drop-in replacement for Android’s standard ImageView which additionally allows images to be loaded from URLs or the user’s contact address book. Images are cached to memory and to disk for super fast loading.
https://github.com/loopj/android-smart-image-view
Or on OnResume of ur activity start downloading the image form the url as u r doing now in the click listener of button in a separate thread to avoid blocking main UI thread. Once you completed the download you can update the image view using handler from the worker thread.
You can make use of async task also instead of creating your own thread and handler to update UI. for more infor about async task refer following link http://www.vogella.com/articles/AndroidPerformance/article.html
I have a custom listview that contains images and text from the server. If server returns 500+ sets of data I can render all the data to my listview but it will take lots of time to load
Instead I should render the data when list is scrolled down. For this I was return the some code to load the images but still I was not satisfied with this code. I am loading the images from the getView() method by calling manager.fetchBitMapThread(data.getImgUrl(),iv) (ImageLoaderThread). Because of so many threads will create while running. can one suggest the good idea to load the data custom listview.
I have seen OnScrollListener but I am not understanding how to implement this for a custom listview.
manager.fetchBitMapThread(data.getImgUrl(),iv);
public class Manager {
ImageView imageView;
final int stub_id=R.drawable.stub;
private final Map<String, Bitmap> bitMap;
public Manager() {
bitMap = new HashMap<String, Bitmap>();
}
public void fetchBitMapThread(final String urlString, final ImageView imageView) {
this.imageView = imageView;
if (bitMap.containsKey(urlString)) {
imageView.setImageBitmap(bitMap.get(urlString));
}
imageView.setImageResource(stub_id);
final Handler handler = new Handler() {
#Override
public void handleMessage(Message message) {
if(!message.equals(null))
imageView.setImageBitmap((Bitmap) message.obj);
else
imageView.setImageResource(stub_id);
}
};
Thread thread = new Thread() {
#Override
public void run() {
// set imageView to a "pending" image
Bitmap bitM = fetchBitmap(urlString);
Message message = handler.obtainMessage(1, bitM);
handler.sendMessage(message);
}
};
thread.start();
}
public Bitmap fetchBitmap(String urlString) {
if (bitMap.containsKey(urlString)) {
return bitMap.get(urlString);
}
Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
try {
InputStream is = fetch(urlString);
Bitmap drawable = BitmapFactory.decodeStream(is);
if (drawable != null) {
bitMap.put(urlString, drawable);
//Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", " + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", " + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
} else {
//wrong.setImageResource(stub_id);
Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
}
return drawable;
} catch (MalformedURLException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
//imageView.setImageResource(stub_id);
return null;
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
// imageView.setImageResource(stub_id);
return null;
}
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
follow this link to Load images from server in Custom List View.
http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134
Download this and try to implement it in your application.
OnScrollListener provides four arguments which are as below:
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
As the name suggests , first one is first item which is visible to you on screen ,second one is total no of items which are visible to you and last one tells you total no of itema list has which is in your case is 500+ . So send request to download the images for those itema/rows which are visible to user . BY ding this first less threads will be created second plus point is images will be downloaded much faster .Use placeholder or progreess bar for image view .
Prepare one map for bitmap images and in key as url value . In get view method check bitmap for particular image view position is downloaded or not using hashmap and set progress bar visibility according to that .
For doing above you cant do simply creating a thread (in anonymous inner class) , Do it efficiently by creating a class extending thread and implement one listener which notifies both ui thread and background for image downloaded or not . It will be something like creating a pipeline of thread asynchronus .
I hope above approach could help you in loading 500+ rows without taking time else doing all images download in asynchronous can lead to Bitmap out of memory exception and its very much time taking too.
Follow this link
http://android-er.blogspot.in/2010/07/load-listview-in-background-asynctask.html
Complete example to load images from server using Async Task and display them in a ListView
I have an app in which i am downloading and displaying images in a thread. Everything is working fine. But when the images are getting loaded same image is getting loaded in two to three places and when we scroll the screen images are loaded correctly. Please can any one of you can give any suggestions to this?
code:
try{
holder.progress.setVisibility(View.VISIBLE);
DownLoadImageInAThreadHandler(Item, holder);
}
catch(Exception e)
{
System.out.println("Exception in Downloading image : " + e.getMessage());
}
return convertView;
}
//This code is outside the getview method
public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
nImageDownLoads++;
System.out.println("The images being downloaded :" + nImageDownLoads);
final Handler handler = new Handler()
{
#Override public void handleMessage(Message message)
{
holder.imgitem.setImageDrawable((Drawable) message.obj);
holder.imgitem.setVisibility(View.VISIBLE);
holder.progress.setVisibility(View.GONE);
}
};
//Thread for downloading the images
Thread t = new Thread()
{
public void run()
{
try
{
Item.bImageDownLoaded = 2;
System.out.println("Downloading image :"+Item.ImageUrl);
drawable=getDrawableFromUrl(Item.ImageUrl);
nImageDownLoads--;
System.out.println("Downloaded image :" + Item.ImageUrl);
System.out.println("Remaining images for downloading: " + nImageDownLoads);
if(drawable != null)
{
Item.bImageDownLoaded = 1;
//Send the message to the handler
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
else{
int idNoImage = R.drawable.giftsuggestionsnoimage;
Drawable dwgNoImg = getParent().getResources().getDrawable(idNoImage);
//Send the message to the handler
Message message = handler.obtainMessage(1, dwgNoImg);
handler.sendMessage(message);
}
}
catch(Exception exp)
{
System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
}
}
private Drawable getDrawableFromUrl(String imageUrl) throws IOException
{
try
{
image = DownloadDrawable(imageUrl, "src");
//bmp = readBitmapFromNetwork(new URL(imageUrl));
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
//return bmp;
}
};
t.start();
}
Drawable DownloadDrawable(String url, String src_name) throws java.io.IOException
{
return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), src_name);
}
Are you using a ListView? if so, is it possible that the view is already recycled by ListView (reused to display another list item) by the time that you write the image in it?
Problem happens because you have written the code to load the images in getView() method.
And that method get called every time you scroll the list.
So that's a poor and not good idea at all.
Remove that code from there..
Load images and in other thread or AsyncTask
And then call notifyDataSetChanged() of you Adapter class object.
Just steps for what I indicated to do, Please read carefully and try to implement, if cant understand anything feel free to ask..sorry couldnt paste any code snippet..
ListView adapter's getView() method
show default icon if array doesn't contain bitmap at that position
if bitmap is there show bitmap else show default icon
AsyncTask to Load Data
do in background
load data and show in listview
AsyncTask to Load Images
do in background
download images one bye one from the urls got in first AsyncTask
in onPostExecute call adapter's notifyDataSetChanged() to show the bitmaps too
you can also call onProgressUpdate periodically means..after 2-3 image download
call publishProgress in doInBackground which will call onProgressUpdate and
in that method also write adpater.notifyDataSetChanged() to
reflect currently downloaded/available bitmaps in array
I am having a customized list view in my application, which is showing an image and text.
The Image I am getting from URL, using the code below:
private static Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
all is working perfect, except the list view scrolling, its very slow. If I disable the images, the scroll speed smooth-ens , but with the image enabled, it lags alot.
Is there any possible way I can reduce or remove this lagging?
Thanks
You need to do your fetching in the background.
One of the examples you can use :
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
use this library for downloading images in background and caching..
it won't hurt the UI
https://github.com/koush/UrlImageViewHelper
Are you lazy loading your images? See this question for details.