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 8474835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:43:51+00:00 2026-06-10T17:43:51+00:00

I am getting an image via URL from the Internet and trying to resize

  • 0

I am getting an image via URL from the Internet and trying to resize (to a smaller size) before saving it. I managed to save it, but I’m unable to resize it. How could I do that? Here is the code:

URL url = new URL(LogoURL);

InputStream input = url.openStream();
try {
    OutputStream output = new FileOutputStream("data/data/com.android.mylogo/logo.jpg");
    try {
        //byte[] buffer = new byte[aReasonableSize];     
        int bytesRead = 0;
        System.out.println("Buffer Length is \t:-" + buffer.length);
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            System.out.println("inside while");
            output.write(buffer, 0, bytesRead);
        }
    } finally {
        output.close();
        System.out.println("saved image");
    }
} finally {
    input.close();
}
  • 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-06-10T17:43:53+00:00Added an answer on June 10, 2026 at 5:43 pm

    If you want to downscale the image to particular dimensions, you can use the following code:

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(LogoURL);
    
    try {
        HttpResponse response = client.execute(request);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w(LOG_TAG, "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }
    
        HttpEntity entity = response.getEntity();
        if (entity != null) {
    
            InputStream is = null;
            BufferedInputStream bis = null;
            try {
                is = url.openStream();
                bis = new BufferedInputStream(is);
    
                int sampleSize = 1;
    
                bis.mark(Integer.MAX_VALUE);
    
                Options bounds = new Options();
                bounds.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(bis, null, bounds);
                if (bounds.outWidth != -1) {
                    int width = bounds.outWidth;
                    int height = bounds.outHeight;
                    boolean withinBounds = width <= YOUR_DESIRED_WIDTH && height <= YOUR_DESIRED_HEIGHT;
    
                    int newWidth = width;
                    int newHeight = height;
                    while (!withinBounds) {
                        newWidth /= 2;
                        newHeight /= 2;
                        sampleSize *= 2;
                        withinBounds = newWidth <= YOUR_DESIRED_WIDTH && newHeight <= YOUR_DESIRED_HEIGHT;
                    }
                } else {
                    Log.w(LOG_TAG, "Can't open bitmap at " + url);
                    return null;
                }
    
                try {
                    bis.reset();
                } catch (IOException e) {
                    if(is != null){
                        is.close();
                    }
    
                    if(bis != null){
                        bis.close();
                    }
    
                    if(!entity.isRepeatable()){
                        entity.consumeContent();
                        response = client.execute(request);
                        entity = response.getEntity();
                    }
    
                    is = entity.getContent();
                    bis = new BufferedInputStream(is);
                }
    
                Options opts = new Options();
                opts.inSampleSize = sampleSize;
                Bitmap bm = BitmapFactory.decodeStream(bis, null, opts);
    
                return bm;
            } finally {
                if (is != null) {
                    is.close();
                }               
    
                if (bis != null) {
                    bis.close();
                }
    
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        request.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        request.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        request.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    }
    

    When you open the image with Options bounds = new Options(); bounds.inJustDecodeBounds = true;, then the image data won’t be downloaded, only the size of the image. I use this size to calculate the new scale ratio to get the desired width and height.

    With the option Options opts = new Options(); opts.inSampleSize = sampleSize; the BitmapFactory will download an already resized image. You save memory, and bandwidth this way.

    Note, that the sampleSize values should be powers of 2. It works with different numbers as well, but this is much more efficient.

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

Sidebar

Related Questions

I am getting the image size using javascript, but it won't return the correct
I there a way to getting picture(image) from URL without downloading? I use the
I'm trying to resize an image that is uploaded to Drupal via a form.
Via curl from url i getting some info and i need to check if
I'm trying to send an image via bluetooth, i keep getting an error saying
I am having following code for getting image from the web: NSURL *ImageURL =
I learned getting the image size using itextsharp pdf the image size is the
The above emulator image is just getting the input from the user and showing
Trying to create a background-image slideshow and am getting this error... This is the
I have tried to upload image using FileTransfer.upload in phonegap api.But I am getting

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.