I want to use the image directly from website. I did not place that image in drawable folder. For this purpose what can I do?
Please, give me some suggestions.
I've written a function that creates a Drawable from an image url, look below:
private Drawable downloadThumbnails(String url) {
Drawable d = null;
try {
InputStream is = (InputStream) this.fetch(url);
d = Drawable.createFromStream(is, "src_from_stream");
return d;
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
then use it like this:
Drawable img = downloadThumbnails("http://yoururl.com/image.png");
yourImageView.setImageDrawable(img);
I strongly recommend using Picasso:
Picasso.with(context).load("http://an.url.com/lovely_cats.png").into(imageView);
It has a lot of usefull functions that allows you to:
crop the image
resize the image
rotate the image
fit an image into the target view
replace not downloaded file with local placeholder
an even more - mostly in a single line (or call).
Related
I have URL to PNG image file.
I want to get this image file and set it as a source for an ImageView.
My code:
URL iconURL = null;
try {
iconURL = new URL("https://maps.gstatic.com/mapfiles/place_api/icons/worship_general-71.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap icon = null;
try {
icon = BitmapFactory.decodeStream(iconURL.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
myImageView.setImageBitmap(icon);
If I place a breakpoint into the line with decodeStream() the app stops and I see the following:
If I run the app in regular mode, it just stops!
Can someone explain what is wrong here and how to do it correctly?
Thanks.
If you just want to show the image on your ImageView then you can use Picasso Library.
Put below dependency in build.gradle file
'com.squareup.picasso:picasso:2.71828'
And use below java code for loading your image from URL to your ImageView
Picasso.get()
.load(url)
.into(imageView);
know anyone of you how can I use one photo from URL with out download? Because I can have a lot of images and if my application CANT download all of them...
I've found one function on web he how can do this but it don't work.
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;
}
}
I really need that, so have anyone any idea?
You can use Picasso lib for downloading image from url:
http://square.github.io/picasso/
Example:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
I want download some images from an android application, and then show these images.
I want know where I have to put my downloaded images and how to refer to them in order to show them, for example using them as background of a View object.
Here is an example of downloading an image from a URL and showing it in an ImageView.
This is the download method:
void downloadFile(String fileUrl) {
try{
InputStream is = (InputStream) new URL(fileUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
imgView.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
}
I've been trying to develop an android app that parses through a specific (business's) website, and display the head image of the site on the top, news on the bottom, and have buttons below it that will open a new activity to display the schedule for the business, and any other information about the business. I decided to use JSOUP, which I am very new to, but I can't figure out how it is that I go about displaying images. I tried something like this, but it didn't work:
ImageView image = (ImageView) findViewById(R.id.headImage);
Document doc = Jsoup.connect("http://www.example.com/").get();
Elements divs = doc.select("img");
for (Element div : divs) {
Log.d("web Stuff",div.text());
text.setText(text.getText() + "\n" + div.text());
Element myImage = div;
String url = myImage.absUrl("src");
image.setImageDrawable(Drawable.createFromPath(url));
}
how am I supposed to correctly implement something like this?
I would load the image in a new thread if you want to do it for more than one image.
public void run() {
URL url = new URL(url);
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
}
when the Thread is finished you can set the image of the imageview.
image.setImageBitmap(bitmap);
Drawable drw =LoadImageFromWebOperations(image_url);
im.setImageDrawable(drw);
private Drawable LoadImageFromWebOperations(String strPhotoUrl)
{
try
{
InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
I have an image in one of my Activities, and this is control by a remote server. My problem is, whenever I use this code, the image appears smaller on my phone but not in emulator. I want the image to appear just like its original size. Can someone tell me what I am doing wrong. Thanks in advance!
ImageView imgView =(ImageView )findViewById(R.id.image01);
Drawable drawable = LoadImageFromWebOperations("http://www.yourimage.com");
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;
}
}
Here is my xml:
<ImageView
android:id="#+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
you can try changing drawable to Bitmap with commant:
(BitmapDrawable)drawable).getBitmap())
and change the size of bitmap
b = Bitmap.createScaledBitmap(((BitmapDrawable)drawicon).getBitmap(), your_size_W, your_size_H, true);
and use: setImageBitmap to show in ImageView