Android Drawable Images from URL

Related searches

I am presently using the following piece of code to load in images as drawable objects form a URL.

Drawable drawable_from_url(String url, String src_name) 
throws java.net.MalformedURLException, java.io.IOException {
        return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);

}

This code works exactly as wanted, but there appears to be compatibility problems with it. In version 1.5, it throws a FileNotFoundException when I give it a URL. In 2.2, given the exact same URL, it works fine. The following URL is a sample input I am giving this function.

http://bks6.books.google.com/books?id=aH7BPTrwNXUC&printsec=frontcover&img=1&zoom=5&edge=curl&sig=ACfU3U2aQRnAX2o2ny2xFC1GmVn22almpg

How would I load in images in a way that is compatible across the board from a URL?

Bitmap is not a Drawable. If you really need a Drawable do this:

public static Drawable drawableFromUrl(String url) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    x = BitmapFactory.decodeStream(input);
    return new BitmapDrawable(Resources.getSystem(), x);
}

(I used the tip found in https://stackoverflow.com/a/2416360/450148)

How to Convert Image url into Drawable Int, How to Convert Image url into Drawable Int � android json. Good Morning All !!!! I am trying to display images in Imageview. But My requirement is� Questions: I am presently using the following piece of code to load in images as drawable objects form a URL. Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException { return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name); } This code works exactly as wanted, but there appears to be

Solved it myself. I loaded it in as a bitmap using the following code.

Bitmap drawable_from_url(String url) throws java.net.MalformedURLException, java.io.IOException {

    HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();
    connection.setRequestProperty("User-agent","Mozilla/4.0");

    connection.connect();
    InputStream input = connection.getInputStream();

    return BitmapFactory.decodeStream(input);
}

It was also important to add in the user agent, as googlebooks denies access if it is absent

Android Image Loading from a String URL | by Chris Ross, Occasionally, adding images to your android application is simple and straightforward, and all you have to do is place the image in the drawable folder and set the ImageView source (“src”) property in your layout xml file to the correct path. I am adding a couple of sample items in my application so it doesn't look so empty when the user look at it the first time. The list with the sample items should have an image and the image I am go

I'm not sure, but I think that Drawable.createFromStream() is more intended for use with local files rather than downloaded InputStreams. Try using BitmapFactory.decodeStream(), then wrapping the return Bitmap in a BitmapDrawable.

get Drawable From Url : Drawable � 2D Graphics � Android, Browse other questions tagged android url kotlin android-asynctask android-drawable or ask your own question. The Overflow Blog Podcast 261: Leveling up with Personal Development Nerds

The following code works for me:

Matrix Mat = new Matrix();

Bitmap Source = BitmapFactory.decodeFile("ItemImagePath");

Bitmap Destination = Bitmap.createScaledBitmap( Source, 320, 320, true );

Source = Bitmap.createBitmap( Destination, 0, 0, Destination.getWidth(), Destination.getHeight(),Mat, true );

ItemImageView.setImageBitmap(Source);

I want to display image on screen. Image should come from URL, and not drawable. Code is here: <ImageView android:id="@+id/ImageView01" android:src = "http://l

You can use com.androidquery.AndroidQuery to do this quite simply. For example:

AQuery aq = new AQuery(this);
aq.id(view).image("http://yourserver/yourimage.png", true, true, 300, new BitmapAjaxCallback() {
        @Override
        public void callback(String url, ImageView imageView, Bitmap bitmap, AjaxStatus status) {
            Drawable drawable = new BitmapDrawable(getResources(), bm);
        }
    });

If you use the BitmapAjaxCallback you will get access to the BitMap which you can wrap as a BitmapDrawable.

Image url is coming perfectly. But the image is not getting download by Glide. I am calling the above line inside onBindViewHolder() of an Adapter. Earlier it was working with v3 – sagar suri Jul 21 '17 at 8:32

// Imageview to show ImageView imageView = new ImageView(context); // ImageLoader class instance ImageLoader imgLoader = new ImageLoader(getApplicationContext()); // Loader image - will be shown before loading image int loader = R.drawable.loader; // whenever you want to load an image from url // call DisplayImage function // url - image url to load // loader - loader image, will be displayed before getting image // image - ImageView imgLoader.DisplayImage(imagUrl[position], loader, imageView );

In an android image slider with a slideshow using a Viewpager example, you will learn how to create an image slider in the Android app. Quick links to various examples of image sliders and android app into tutorials. 1. Android Image Slider With Indicator Example. 2. Image Slider From URL in Android. 3. Android App Intro Image Slider Example. 4.

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon.

Comments
  • Great answer, but note that the constructor is deprecated. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density
  • great, answered. should be updated due to deprecated
  • It's Throws Exception like android.os.NetworkOnMainThreadException
  • This is incorrect, @Prince see my answer using an AsyncTask to avoid NetworkOnMainThreadException.
  • I seem to remember running into that problem before; I was on 1.5 at the time.
  • The person asked for some way to create a drawable from an url,yours one is doing that underneath,no way of getting the drawable;rather you pass a view and the library sets the image on it which is downloaded from the url.So how it can be considered to be an answer for the question?