0

I am trying to get the Bitmap from a URL from the StaticConfig.getMyUser().getAvatar(). The url is as follows "https://lh6.googleusercontent.com/-0feEeEohl8I/AAAAAAAAAAI/AAAAAAAAAGA/htLteFBdk5M/s96-c/photo.jpg".

When I use the code below, src is null. I'm not sure why. I am currently looking into this link but I'm still clueless to what I should do.

private void setImageAvatar(Context context){
    Resources res = getResources();
    Bitmap src;
    byte[] decodedString = Base64.decode(StaticConfig.getMyUser().getAvatar(), Base64.DEFAULT);
    src = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    if(src == null){
        Log.e("UserProf","src is null");
    }
    // code to do something with src here
}
CraftedGaming
  • 499
  • 7
  • 21

3 Answers3

0

At first you need to download image from that url. It's better to use a image loader like Glide

or Picasso

islamdidarmd
  • 725
  • 5
  • 19
0

As suggested by others, the ideal way would be to use Picasso or Glide.

Because it handles many things, from caching to memory optimisation for you. for eg. with Glide you will just have to write

Glide.with(context)
     .load("https://lh6.googleusercontent.com/-0feEeEohl8I/AAAAAAAAAAI/AAAAAAAAAGA/htLteFBdk5M/s96-c/photo.jpg")
     .into(imgview);

Or If you want to write manually, you can use below method. imageview is instance of initialized Imageview.

public void setBitmapFromNetwork(){

Runnable runnable = new Runnable() {
    Bitmap bitmap = null;

    @Override
    public void run() {
        try {
            bitmap = getBitmapFromLink();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageview.post(new Runnable() {
            @Override
            public void run() {
                imageview.setImageBitmap(bitmap);
        }});
    }};
    new Thread(runnable).start();
}


public Bitmap getBitmapFromLink() throws IOException{
    HttpURLConnection conn =(HttpURLConnection) new URL("https://lh6.googleusercontent.com/-0feEeEohl8I/AAAAAAAAAAI/AAAAAAAAAGA/htLteFBdk5M/s96-c/photo.jpg").openConnection());
    conn.setDoInput(true);
    InputStream ism = conn.getInputStream();
    Bitmap bitmap = BitmapFactory.decodeStream(ism);
    if (ism != null) {
        ism .close();
    }
    return bitmap;
}

And please make sure you have given Internet permission in manifest.

CraftedGaming
  • 499
  • 7
  • 21
Man
  • 2,720
  • 2
  • 13
  • 21
  • did you mean `openConnection()`? If so, it's giving me an error. `Incompatible types. Required: java.net.HttpURLConnection Found: java.net.URLConnection` – CraftedGaming Nov 05 '17 at 08:41
  • Pardon me. Actually i had tired in kotlin . forgot to typecast in java. Check the updated answer. – Man Nov 05 '17 at 08:46
  • Thanks for the code. Apparently, this code is printing out a `NetworkOnMainThreadException`. I created a new thread but it only generated more errors – CraftedGaming Nov 05 '17 at 09:29
  • I didn't find any issue with previous method in thread. Check the updated answer with complete code you need. – Man Nov 05 '17 at 11:41
0

Just in case you want to use kotlin version.

class SetBitmapFromNetwork(img: ImageView?): AsyncTask<Void, Void, Bitmap>() {

    var img:ImageView? = img

    override fun doInBackground(vararg p0: Void?): Bitmap {

        val conn: HttpURLConnection = (URL("https://lh6.googleusercontent.com/-0feEeEohl8I/AAAAAAAAAAI/AAAAAAAAAGA/htLteFBdk5M/s96-c/photo.jpg").openConnection()) as HttpURLConnection

        conn.doInput = true

        val ism: InputStream = conn.inputStream

        return BitmapFactory.decodeStream(ism)


    }

    override fun onPostExecute(result: Bitmap?) {
        super.onPostExecute(result)
        img?.setImageBitmap(result)
    }
}

Just call as,

SetBitmapFromNetwork(img).execute()
Man
  • 2,720
  • 2
  • 13
  • 21