Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get byte Image From url in Android

I am new to android.The Image is store in server by Base64 format. so how can i get it from server to My Project and set to my ImageView using Json Object. Please Help me

Any help will be Appappreciated


1 Answers

Try this:

Convert Url to byte[] first:

byte[] bitmapdata = getByteArrayImage(url);

Method:

private byte[] getByteArrayImage(String url){
         try {
                 URL imageUrl = new URL(url);
                 URLConnection ucon = imageUrl.openConnection();

                 InputStream is = ucon.getInputStream();
                 BufferedInputStream bis = new BufferedInputStream(is);

                 ByteArrayBuffer baf = new ByteArrayBuffer(500);
                 int current = 0;
                 while ((current = bis.read()) != -1) {
                         baf.append((byte) current);
                 }

                 return baf.toByteArray();
         } catch (Exception e) {
                 Log.d("ImageManager", "Error: " + e.toString());
         }
         return null;
    }

Now convert the byte[] to bitmap

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);

And set your bitmap to your ImageView:

img= (ImageView) findViewById(R.id.imgView);
img.setImageBitmap(bitmap );
like image 104
Jas Avatar answered Jan 25 '26 02:01

Jas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!