MrOut
MrOut

Reputation: 23

Convert image URL to drawable resource id in android

I'm using the KenBurnsView library with this code:

mHeaderPicture.setResourceIds(R.drawable.picture0, R.drawable.picture1);

As you can see it takes drawable resource ids.

What I want to do is covert all photos URLS to drawable resource ids. Photo urls like this:

http://example.com/image.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg

I tried this code here and it didn't work:

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

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

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

    x = BitmapFactory.decodeStream(input);
    return x;
}

I have read a LOT of questions and answers all over the web and I didn't find a good tutorial or solution.

Upvotes: 1

Views: 14170

Answers (4)

samgak
samgak

Reputation: 24417

You can't convert a Bitmap to a resource id. Resource ids are only for resources in your APK. You'll have to edit or extend the KenBurnsView class to accept Bitmap objects e.g by adding a function like this:

public void setBitmaps(Bitmap... bitmaps) {
    for (int i = 0; i < mImageViews.length; i++) {
        mImageViews[i].setImageBitmap(bitmaps[i]);
    }
}

Then you can pass the Bitmaps that you load with drawable_from_url()

Upvotes: 0

adnbsr
adnbsr

Reputation: 635

You just forgot to add setDoInput to connection

public Bitmap getBitmapFromURL(String src) {
    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;
    }
}

Upvotes: 0

icaneatclouds
icaneatclouds

Reputation: 1170

Try to drop this library into your project. Its a useful library. http://square.github.io/picasso/

Sample Usage:

Picasso.with(context).load("http://example.com/image.jpg").into(imageView);

How to get the drawable from imageView:

Drawable myDrawable = imageView.getDrawable();

Upvotes: 3

Arshid KV
Arshid KV

Reputation: 10037

Add asynchronous task ...

new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute(url);
}
public void onClick(View v) {
    startActivity(new Intent(this, IndexActivity.class));
    finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView mHeaderPicture;
public DownloadImageTask(ImageView mHeaderPicture){
        this.mHeaderPicture= mHeaderPicture;
    }  protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        mHeaderPicture.setImageBitmap(result);
    }
}`

Make sure you have the following permissions set in your AndroidManifest.xml to access the internet.

<uses-permission android:name="android.permission.INTERNET" />

Upvotes: -1

Related Questions