I have several images on some of my Activities, and these are all connected from Remote Server. The problem is every time I go on one of these Activities that contain images from remote sever. All of the images must load first otherwise you will get a black empty screen. Sometimes it takes about 1-5 minutes to load, and sometimes it even force close the device. Is there anyway I can fix this issue?
Here is the code I'm using:
ImageViewimgView =(ImageView)findViewById(R.id.image01);
Drawable drawable = LoadImageFromWebOperations("http://forum.roda.hr/images/customavatars/avatar10164_2.gif");
imgView.setBackgroundDrawable(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;
}
}
I have been struggling for days trying to figure this out, please help me, it would be really mean a lot to me. Thanks in advance!
http://www.androidpeople.com/android-load-image-url-example
this should help..
and as jett try doing it in lazy with AsynTask to do the loading in background..
The key point is that you should use Thread to load the images from Internet.
Please refer to Android - How do I do a lazy load of images in ListView to achieve this.
Better to use thread to download image.
Refer this
Load Image from server using thread
Painless Threading
Related
In my app I am able to pick up a picture from the Gallery and show it on my phone. The picture has extension JPG. But when I email it to myself, save it on the server and then try to display it on my phone, it does not display. I even tried to downsize it to 30% using my email app on the phone, so now it is 220KB instead of 1.4MB but it still does not display.
In both cases I use the method
imageView.setImageBitmap(BitmapFactory.decodeFile(personPicture))
What do I need to do to overcome this problem?
BTW: the name of the picture was changed when I saved it on the server. I do not think it matters but I mentioned it anyway.
EDIT
The above is all the code I am using. Just to complete the issue here is the code that handles both jpg and png and it works if the picture is renamed to png.
if (url.contains("jpg")) {
imageView.setImageBitmap(BitmapFactory.decodeFile(url));
} else {
Drawable drw = LoadImageFromWebOperations(url);
if (drw != null) {
imageView.setImageDrawable(drw);
}
}
Note: the 1.4MB PNG file worked fine on the emulator but gave Out of memory exception on the device. When I re-sized the PNG file to 350KB it displayed properly on the device also.
If needed here is the url used in the above code (a picture of a cat)/
http://212.150.56.58:8080/feedback/pictures/56.png
When you try to load image from server into app, load it using Picasso library like as below:
Picasso.with(MainActivity.this).load("image_to_be_loaded").into(profile_image);
Edit
If you don't want to use third party library then try the following code:
public Drawable loadImageFromURL(String url, String name) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, name);
return d;
} catch (Exception e) {
return null;
}
}
Use Picasso to load server images to your app image view it also supports caching and lot many features.
Use is like this
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
checkout Picasso Library for more details.
I do not understand why, but if I renamed the picture on my server to extension PNG the picture displays, in all sizes.
I have a code similar to this.
And like in there I want to add pictures dynamically to my ListView using a SimpleAdapter, but instead of having the photos on the Drawable resources I want to grab mine from the web. I already have a method that gets a url and returns a Drawable and I want to add the id of that Drawable to the HashMap like he does. How to I get that id (R.drawable.blabla) ?
Btw here's the method
public static Drawable loadImageFromWeb(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
Adding a new Drawable Resource at runtime is not possible. Resource ID's and data are generated while editing and bundled into your APK at build time.
Instead create a local cache and load files from there.
Whatever that guy is doing isn't the best way to go about it, use a https://developer.android.com/reference/android/widget/BaseAdapter.html base adapter and override the getview method
Ok, managed to solve the issue with this:
http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/
Very good source, thanks all.
I'm building an app for a friends funny pictures website. I downloaded the gridview activity from the android developers website, but it just loads preset image urls, and that's simply not going to work for us. We need an image loader that can automatically load images from the site.
Any help would be greatly appreciated.
Use lazylist download
https://github.com/thest1/LazyList
Lazy load of images in ListView
Hope AQuery library is what you are looking for easy image loading on ImageView
Try out this
Drawable drawable = LoadImage(<Replace this with your url> + UserProfile.Photo);
UserPhotoImageView.setImageDrawable(drawable);
<replace with Your Image View>.setImageDrawable(drawable);
public Drawable LoadImage(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;
}
}
Rather than what others are suggesting, I think the best way to do this is use a UniversalImageLoader. It will allow you to Lazy Load, and cache on the SD card and internal storage. In short, it makes things simpler and allows you to get the fastest speed possible. Try it out.
I'm aware that the line
Drawable.createFromStream(fileInputStream, "src");
is capable of throwing an OutOfMemory error when the app's memory limit has been reached. However, I'd like to think I've been very diligent when it comes to being efficient with memory.
Here is the the block which actually retrieves the drawable:
if(file.exists()) {
//Get image from app files, null if it doesn't exist
try {
FileInputStream fis = context.openFileInput(filename);
drawable = Drawable.createFromStream(fis, "src");
fis.close();
if(!saveInCache)
mInstanceCache.put(urlString, drawable);
return drawable;
} catch(Exception e) {
e.printStackTrace();
}
}
I have never been able to reproduce the OutOfMemory exception on my end, but I get 2-3 reports a day from users who do run into it. I'd like to make my code more efficient, but I'm out of ideas. I already do the following:
1) Add small images (listview sized) to a cache which is cleared as soon as you leave that listview.
2) Always clear the ImageView's drawable and run garbage collection whenever an image is either replaced with a new one, or onStop() of the activity hosting the large image is called.
I thought that would be enough, but I guess not. One thing I thought of doing was switching to Bitmaps instead of drawables (using Android's own BitmapFactory). Would that even make a difference in terms of memory/efficiency?
Any feedback is appreciated. Thanks!
Try creating the drawable from a Bitmap, using the inPurgeable option from BitmapFactory.Options. I remember having the same issue, and solved it that way.
The image should be big thats why phones that have low ram give OutOfMemory Exception.
Try to reduce the size and resolution of the image if you can. Have a look at http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I am receiving images from a URL and they display on the emulator but when I use a device the default image is shown. Im not totally sure which part of the code you require so Ill add more on request:
pubImage = extras.getString("pubImage");
ImageView ivimage = (ImageView) findViewById(R.id.image);
try{
ivimage.setImageDrawable(grabImageDrawableFromUrl(pubImage));
Log.d(TAG, pubImage);
}catch(Exception e){
e.printStackTrace();
}
I use Prime for all of my image loading in Android. If you were using it then you would not have to worry about issues like this.
try this...
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
ivimage.setImageDrawable(drawable);
if you still not get solution then use Universal Image Loader
Look at the answer to this question, it seems as if you might be missing a few details, but perhaps you should paste grabImageDrawableFromUrl() either way.