Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4171608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:27:53+00:00 2026-05-21T00:27:53+00:00

I’m trying to download a jpg image from the web saving it into a

  • 0

I’m trying to download a jpg image from the web saving it into a byte array in order to have the compressed image on the ram until I have to show it, but I’m getting a black picture.

What I’m doing is:

URL myFileUrl =null; 

myFileUrl= new URL(fileUrl);

HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
conn.setUseCaches(false);

InputStream is = conn.getInputStream();

byte[] ba = new byte[is.available()];

After that I’m keeping the array in the RAM until I have to use it.

To place it, I do:

Bitmap bitmapFromByteArray = BitmapFactory.decodeByteArray(ba,0,ba.length);

imageView1.setImageBitmap(bitmapFromByteArray);

bitmapFromByteArray.recycle();

I solved the problem by doing

URL myFileUrl =null; 

myFileUrl= new URL(fileUrl);

HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();

InputStream is = conn.getInputStream();

Bitmap bm = BitmapFactory.decodeStream(is);

int size = bm.getWidth() * bm.getHeight();

ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);

byte[] ba2 = out.toByteArray();
bm.recycle()

Buut by doing this I’m decoding the JPEG and then re-encoding the image multiple times lowering the quality of the picture and using the phone resources longer than necessary. Also, the bitmap I get after re-encoding is bigger than the original jpg image.

Is there any way to get the first method to work?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:27:54+00:00Added an answer on May 21, 2026 at 12:27 am

    Are you trying to simply copy the content of an InputStream into a byte array?

    You can do this directly with a loop that calls InputStream#read into a byte array, or you can leave the implementation up to others by using IOUtils.toByteArray from Apache commons.

    Edit – You don’t need to deal with array initialization if you use the Apache commons method:

    byte[] bytes = null;
    try {
        bytes = IOUtils.toByteArray(conn.getInputStream());
    } catch (IOException e) {
        // handle the exception
    }
    

    toByteArray uses a ByteArrayOutputStream to avoid allocating an array up-front, but you wouldn’t need to worry about this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to download images (.jpg) from web folder using wget. I want to
I am trying to download the image from the url http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg I am using
I am trying to download image from HTTP URL to my computer via c#.
I am trying to download image from internet using open-uri. Here is code: require
I am trying to download a set of .jpg's from Amazon S3 and store
I am trying to download this image using C# http://www.pinkice.com/data/product_image/1/13954Untitled-1.jpg When I try to
I'm trying to download an image using this code: from urllib import urlretrieve urlretrieve('http://gdimitriou.eu/wp-content/uploads/2008/04/google-image-search.jpg',
Im trying to download profile_image_url images (.jpg) from twitter and display them in my
I am trying to download an image from an url...My code is : String
I am trying to download and show an image from my webservice but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.