Display image button from external source - android

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.

Related

Load Images from Assets in Widget remoteView

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;
}

How to display gif image from remote

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.

Bitmap from url without scaling

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.

how to load image from web?

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;
}
}

NinePatchDrawable from Drawable.createFromPath or FileInputStream

I cannot get a nine patch drawable to work (i.e. it is stretching the nine patch image) when loading from either Drawable.createFromPath or from a number of other methods using an InputStream.
Loading the same nine patch works fine when loading from when resources. Here's the methods I am trying:
Button b = (Button) findViewById(R.id.Button01);
Drawable d = null;
//From Resources (WORKS!)
d = getResources().getDrawable(R.drawable.test);
//From Raw Resources (Doesn't Work)
InputStream is = getResources().openRawResource(R.raw.test);
d = Drawable.createFromStream(is, null);
//From Assets (Doesn't Work)
try {
InputStream is = getResources().getAssets().open("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From FileInputStream (Doesn't Work)
try {
FileInputStream is = openFileInput("test.9.png");
d = Drawable.createFromStream(is, null);
} catch (Exception e) {e.printStackTrace();}
//From File path (Doesn't Work)
d = Drawable.createFromPath(getFilesDir() + "/test.9.png");
if (d != null) b.setBackgroundDrawable(d);
I answered a very similar question here about non-resource-based ninepatches. To read a file from within .apk archives, I use something like this:
String filepath = "com/kanzure/images/thx1138.png";
InputStream stream = getClass().getClassLoader().getResourceAsStream(filepath);
I have a very similar problem. I'm trying to create a NinePatchDrawable from a Bitmap that I've retrieved over the network. I still haven't found a solution to that problem. Since the Drawable.createFrom... methods apparently doesn't work, my only option seems to be to understand how to format the nine-patch chunk, and to invoke the NinePatch constructor. See the SO-thread for details.

Categories

Resources