0

I'm trying to get Bitmap from URL. I use the methos:

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

But I cannot pass the line: "connection.connect();". Is there sth which I'm missing? How can I connect()?

My string src = "https://i.stack.imgur.com/I386R.png"

I read that i should " This should be used in AsyncTask derived Class.". How to do it?

tauri
  • 293
  • 2
  • 6
  • 18

2 Answers2

0

Provide more info about the error you are getting. Besides, it might sound stupid but, did you set internet privileges in your manifest file?

Quark
  • 402
  • 4
  • 15
  • Do you mean permission? I have: – tauri Mar 29 '15 at 01:00
  • Then your problem is that you are not opening your connection asynchronously. In order to avoid blocking the main thread, Android wont let you open a connection outside a working thread. Thus, create a new AsyncTask or a new Thread. – Quark Mar 29 '15 at 01:06
  • 1
    @Quark your answer seems to be like a question. If you have any question or request, you could put in the comment of question and not as an answer. – Fahim Mar 29 '15 at 03:12
0

Try it like this: URL imageUrl = new URL(url); Bitmap img = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream()); Also make sure you have the <uses-permission android:name="android.permission.INTERNET" /> permission set in your manifest. An example for AsyncTask to retrieve image would be like:

public class RetrieveImageAsync extends AsyncTask<Void, Void, Bitmap>
{

private String url;

public RetrieveImageAsync(String imageUrl)
{
    this.url = imageUrl;
}

@Override
protected Bitmap doInBackground(Void... params)
{
    Bitmap retVal = null;
    try
    {
        URL imageUrl = new URL(url);
        return BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
    }
    catch (Exception ex)
    {
    }
    return retVal;
}

@Override
protected void onPostExecute(Bitmap result)
{
    try
    {
        //Do something with the result Bitmap
    }
    catch (Exception e)
    {
    }
}

@Override
protected void onPreExecute() {}

@Override
protected void onProgressUpdate(Void... values) {}
}

And then you can initialize it like: new RetrieveImageAsync("http://i.imgur.com/77H07kP.png").execute();

Filipe Silva
  • 220
  • 4
  • 10
  • Thank you for your answer. I tried this solution. The problem is that I want to use it in GridView adapter. I took out from the class the "Bitmap bitmap;" which the doInBackground returns. I call "new RetrieveImageAsync(url).execute();" for each item in GridView. The result is that there is the mess with images. Some of them repeat few times, some don't appear. And they are changing continuously when I scroll GridView. The string info for GridView items are ok, only images are "not matched" to the items. How can I use this method in GridView adapter? – tauri Mar 29 '15 at 01:15
  • That's most likely due to issues with the Adapter. Check this tutorial: http://www.vogella.com/tutorials/AndroidRecyclerView/article.html Also, you might want to consider cashing the images once you've loaded them for better performance – Filipe Silva Mar 29 '15 at 01:17
  • After I set for each item in GridView this "Bitmap bitmap". And it looks that images and randomly mixing in my GridView. Should be one image - one GridView item. – tauri Mar 29 '15 at 01:22
  • I'll assume you're using some sort of adapter when filling the items for the gridview. Whenever you're loading the image async try and set the imageview image to null before calling in the async. Also make sure that you're setting the image in the correct view (you could pass the Imageview instance to the async task and set the image on the postexecute) – Filipe Silva Mar 29 '15 at 05:32
  • Thank you Filipe Silva. All you wrote is true. Anyway, I decided to use Picasso. – tauri Mar 29 '15 at 12:34