I need to insert in a TextView a drawable that I download from web in the code. So I can't manually put the image in res and in R.drawable.
How can I use it?
Steps:
1) Download image
2) Convert to drawable
3) Set drawable to textView by calling smth like this:
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
Try this
try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL("ur Image URL");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Drawable d =new BitmapDrawable(bm);
d.setId("1");
textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
} catch (IOException e) {
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
Reference url How to display an image from an URL within textView
Related
I have a string which as follows:
String text = "<img src="https://www.google.com/images/srpr/logo3w.png">"
When I do
TextView.setText(Html.fromHtml(text));
I need the image to be displayed in the activity.
I do not want to use a WebView.
Please let me know as to what are the other ways to achieve this.
you have to use asynctask,open connection in doInbackground() set image to textview in onPostExecute()
try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL("ur Image URL");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Drawable d =new BitmapDrawable(bm);
d.setId("1");
textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
} catch (IOException e) {
Log.e("error", "Remote Image Exception", e);
}
Hope it will help.
I followed the idea in these below links.
http://www.docstoc.com/docs/120417139/How-To-Implement-Htmlfromhtml-With-Imagegetter-In-Android
http://shiamiprogrammingnotes.blogspot.com/2010/09/textview-with-html-content-with-images.html
You need to define your text in strings.xml
<string name="nice_html">
<![CDATA[<img src='https://www.google.com/images/srpr/logo3w.png'>
]]></string>
Then, in your code:
TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html)));
I have a database online that gives image file locations and file names
I am trying to update an imageview in an android screen. Here is my code that gets the image and the things I have tried:
// successfully received product details
JSONArray productObj = json.getJSONArray("product"); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// imageview
imageVw = (ImageView) findViewById(R.id.imageView1);
// display data in imageview
//imageStr = "http://somesite.com/images/" + product.getString("imagefile");
imageStr = "file://somesite.com/images/" + product.getString("imagefile");
//imgUri=Uri.parse("file:///data/data/MYFOLDER/myimage.png");
//imgUri=Uri.parse(imageStr);
//imageVw.setImageURI(imgUri);
imageVw.setImageBitmap(BitmapFactory.decodeFile(imageStr));
You can do something like this if you want to get the Bitmap of the image stored on the web. I personally use the library called ImageDownloader. The library is simple to use.
You have to understand that "A URL is a URI but a URI is not a URL. A URL is a specialization of URI that defines the network location of a specific representation for a given resource." so, if your file location is http then you need to make the function like below to get the Bitmap. I use ImageDownloader library because it runs its own thread and also manages some caches for faster image downloads.
private Bitmap getImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
}
My final year project requires me to develop a mobile application which fetches a number of Json values from a server. This is my first experience in android development but sure learned something’s and enjoyed the experience.
I have managed to develop the following.
1-a json parser class which fetches the value.
2-a class which displays these values.
3-a database where I store the json response.
However I'm one step away from completing my project, I cannot display the response string image address as real images (shame on me).
I need to do the following.
1- Parse the string path of the image and display the response as image.
I have spent most of my time trying to find the solution on the web, stack overflow. But no luck so far.
I need to do something like this in order to display these images together with the text descriptions.
I have now reached the cross roads and my knowledge has been tested.Is what i'm trying to do here posible?.
Who can show me the way? ,to this outstanding platform.
if you have the image url as a string, you can load and save the image using something like this:
try {
Bitmap bitmap = null;
File f = new File(filename);
InputStream inputStream = new URL(url).openStream();
OutputStream outputStream = new FileOutputStream(f);
int readlen;
byte[] buf = new byte[1024];
while ((readlen = inputStream.read(buf)) > 0)
outputStream.write(buf, 0, readlen);
outputStream.close();
inputStream.close();
bitmap = BitmapFactory.decodeFile(filename);
return bitmap;
} catch (Exception e) {
return null;
}
For saving, make sure you have appropriate permissions if you're going to save to sdcard.
if you just want to load the image (without saving):
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Error getting bitmap", e);
}
return bm;
Just make sure you fetch the image in a separate thread or using AsyncTask or else you'll get ANRs!
you can try this ImageDownloader class from google. It´s works nice :)
Is an AsynkTask that handle the download and set the bitmap to an ImageView.
ImageDownloader
Usage:
private final ImageDownloader mDownload = new ImageDownloader();
mDownload.download("URL", imageView);
i'm searching for a modified Imageviewclass which downloads a image and displays it.
It should also be possible to display a "download fail"-image and a "load"-image.
If anyone knows a class like this please let me know it.
Thx!
You can use this example, just pass the Activity instance in a constructor and extend the view class instead of the Activity class.
Web Imageview
Hope this helps u:
ImageView i = new ImageView(this);
i.setImageResource(" ur load image" ) // show any load image here..eg. gallery image
try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL("ur Image URL");
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
/* Apply the Bitmap to the ImageView that will be returned. */
i.setImageBitmap(bm);
} catch (IOException e) {
i.setImageResource(R.drawable.error); // Error image here
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
I want to display external image like:
"http://abc.com/image.jpg"
in my android phone application.
can any one guide me how to achieve this?
There are many ways to achieve your request. Basically you have to download the image with an urlrequest and then using the InputStream to create a Bitmap object.
Just a sample code:
URL url = new URL("http://asd.jpg");
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
After you obtain the Bitmap object you can use it on your ImageView for instance
Just another approach to download the image from a url
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}