hello all i have image url getting from web and now i want to display that in may imageview.
pls helm me out.
Thanks In Advance.
Below i mentioned one function that will load image from web and convert it into drawable . So you can set that drawable into ImageView.
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;
}
}
Related
I am using the following code to load my image into Android widget ImageView:
remoteViews.setImageViewResource(R.id.widget_image, R.drawable.image);
I have to save the image in drawable folder. Is it possible to save the image in assets instead and load it to remoteView?
Using BitmapFactory, you can load an image in assets/ as a Bitmap from an InputStream provided by AssetManager, and then use the RemoteViews#setImageViewBitmap() method to set it on your widget.
For example:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
...
try {
InputStream is = context.getAssets().open("image.jpg");
Bitmap bmp = BitmapFactory.decodeStream(is);
remoteViews.setImageViewBitmap(R.id.widget_image, bmp);
}
catch (IOException e) {
e.printStackTrace();
}
Here is the solution :
imageView = (ImageView)findViewById(R.id.iv);
try {
// get input stream
InputStream ims = getAssets().open("download.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
imageView.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
i want to display a gif image from server to image view .
my image is animation(gif) just animation(gif image)
my code is :
ImageViewimgView =(ImageView)findViewById(R.id.image01);
Drawable drawable = LoadImageFromWebOperations("http://www.dariran.com/Ads/Mobile/1.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;
}
}
The easiest way to play gif is WebView. See this: https://stackoverflow.com/a/12839918/3864698
Actually you can play in CustomView too.
I'm working on a project where I need to display image buttons with some images saved in the sd card or from an Url. How can I do this? Or what's best practice?
The objective is to change the image on the button only replacing the file in the sd card.
There are other solutions if I don't know which images will be displayed in future?
THX
Here is how to load an image from a URL into a Drawable object:
InputStream is = (InputStream) new URL("http://my.url/path/to/image").getContent();
Drawable buttonBg = Drawable.createFromStream(is, null);
Then set it as the background:
button.setBackgroundDrawable(buttonBg);
or for API 16+ use:
button.setBackground(buttonBg);
If you want to read from a file, use a FileInputStream like so:
FileInputStream fis = openFileInput("/my/path/to/image");
Drawable buttonBg= Drawable.createFromStream(fis, null);
#carmex
Solved:
ImageButton box1 = (ImageButton)findViewById(R.id.box1);
Drawable drawable = GetImg("path/to/image.jpg");
box1.setBackground(drawable);
private Drawable GetImg(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("Err="+e); return null;
}
}
Thx a lot.
i am getting bitamap from a url. Everything is fine but i m having small issue i.e my bitmap from url gets scales down. I want bitmap of original size without scaling. How to achieve this. I tried options but no success yet. here is my code:
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
I set this drawable to imagview. But image is scaled down. I want original size and original size is not too big so need to worry about outofmemory exception. Is there any way to avoid scaling while parsing bitmap from url stream.
Thanks.
Directly show image from web without downloading it. Please check the below function . It will show the images from the web into your image view.
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
then set image to imageview using code in your activity. Maybe this will help you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to use BitmapFactory.decodeFile method to decode a image from http location?
i have a problem in set url image to image view. i tried below methods
Method 1:
Bitmap bimage= getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
Method 2:
Drawable drawable = LoadImageFromWebOperations(bannerpath);
image.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;
}
}
i tried above two methods but are not working . both are showing DEBUG/skia(266): --- decoder->decode returned false . i used demo url image paths that is working. but this path is not working .so please tell me what is the wrong and what i will do
Thank you in advance.
Best Regards.
Just Tested your "Method 1" in my app and it works just fine.
You might have forgotten this line in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />