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.
Related
Ok, after searching and browsing over the internet, I just didn't get what I was looking for. I have a database with a field for "Path", where my images file names are listed.
**Table1:**
PATH ID
imgRice.jpg 1
imgMango.jpg 2
imgBanana.jpg 3
Now I want my imageView to change picture accordingly from my query:
"Select Path from table1 where ID = 1";
I will declare a String, like String queryResultString to handle the result (ex.imgRice.jpg).
I want a code that looks like imgView.image="drawable/" + queryResultString
Any help is appreciated. Thanks guys!
Thanks for the comments and suggestions guys, highly appreciated.
But I've got a simple yet tricky turnaround for my problem.
First, it is IMPOSSIBLE to rip the Drawable path because it is compressed when published to .apk file.
Second, to make it possible, you need to use id for every file in your drawable.
Third, it is much better to add files(images) to asset folder(\app\src\main\assets).
Lastly, follow these simple codes:
//code to initiate the object ImageView...
ImageView myImage = (ImageView) findViewById(R.id.pic);//pic is the id of ImageView in my xml file...
try
{
// now, get the input stream
InputStream input= getAssets().open(c.getString(c.getColumnIndex("image")));//image is the name of field in my db which holds the path (ex. rice.jpg)...
// initiate image as drawable
Drawable draw = Drawable.createFromStream(input, null);
// set image to ImageView
myImage.setImageDrawable(draw);
}
catch(IOException ex)
{
return;
}
Enjoy Coding! ^_^
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 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.
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
I am facing problem while setting the backGround Image of LinearLayout from asset folder.
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
frontTextView.setBackgroundDrawable(d);
Can someone help me out.
First you create a Drawable object from the asset file:
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
and then set it to some View that only supports Drawables as a background.
As far as I'm aware, you cannot access assets directly like you are trying to. You'll need to use the AssetManager class to get at your data if you want to store it as an asset. Here's a pretty decent blog post explaining a bit about resources and assets, though the official documentation is also a good resource, of course.
I'll also add, though, that things like background images are typically best stored in res/drawable and accessed using the R.drawable.* style (the blog post linked above also discusses this) whenever possible. It's not really clear why you need to do it this way from your provided code sample, though, so I suppose that's ultimately your call.
EDIT: added create image from InputStream...
I had the similar problem using ImageButton. I figured it out by loading bitmap from assets and using it as image for ImageButton. Probably not a good approach, but is working and solved my problem - unability to have subfolders in drawable dir and not allowed characters in file names.
(Yes, I can use prefix instead of subdir, and rename files to match the pattern (lowercase only and numbers) and I probably will do it later.)
InputStream is = null;
try {
is = this.getResources().getAssets().open("Images/Fruits/Apple.png");
} catch (IOException e) {
Log.w("EL", e);
}
Bitmap image = BitmapFactory.decodeStream(is);
ImageButton ib2 = (ImageButton) findViewById( R.id.imageButton2);
ib2.setImageBitmap( image);