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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:39:00+00:00 2026-06-14T23:39:00+00:00

I’m trying to open an image from a specific uri, checking if this image

  • 0

I’m trying to open an image from a specific uri, checking if this image is too big to resize it, and return it as Base64. My problem is the code I have to resize image never resize images (never indicate that the image is too big). I looked for others questions similar this, but I didn’t get any answer.
I don’t know why always in this two code lines the values are -1:

        final int height = options.outHeight; // Always -1 WHY?
    final int width = options.outWidth; // Always -1 WHY?

I attach my code:

private class WriteImage extends AsyncTask<Object, Void, String>{
            private static Bitmap decodeSampledBitmapFromStream(InputStream is,
                                                    int reqWidth,
                                                    int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    InputStream aux = inputStreamCopy(is);
    BitmapFactory.decodeStream(is, null, options);  // SkImageDecoder::Factory returned null
    try {
        is.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(aux, null, options);
}

private static InputStream inputStreamCopy(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len;
    byte[] buffer = new byte[1024];

    try {
        while ((len = is.read(buffer)) > -1) baos.write(buffer, 0, len);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ByteArrayInputStream(baos.toByteArray());
}

private static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight; // Always -1 WHY?
    final int width = options.outWidth; // Always -1 WHY?
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height)
            inSampleSize = Math.round((float)height / (float)reqHeight);
        else
            inSampleSize = Math.round((float)width / (float)reqWidth);

        // This offers some additional logic in case the image has a strange
        // aspect ratio. For example, a panorama may have a much larger
        // width than height. In these cases the total pixels might still
        // end up being too large to fit comfortably in memory, so we should
        // be more aggressive with sample down the image (=larger
        // inSampleSize).
        final float totalPixels = width * height;

        // Anything more than 2x the requested pixels we'll sample down
        // further.
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap)
            inSampleSize++;
    }
    for (int i=5; i>0; i--){
        if (Math.pow(2, i)<=inSampleSize){
            inSampleSize = (int) Math.pow(2, i);
            break;
        }
    }
    return inSampleSize;
}


    @Override
    protected void onPreExecute(){  
    }

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

    @Override
    protected String doInBackground(Object... params) {
        try {
            InputStream is = ((android.app.Activity) params[1]).getContentResolver().openInputStream((Uri) params[0]);

            Bitmap bitmap = decodeSampledBitmapFromStream(is, 300, 300);
            int bytes = bitmap.getWidth()*bitmap.getHeight()*4;
            ByteBuffer buffer = ByteBuffer.allocate(bytes);
            bitmap.copyPixelsToBuffer(buffer);
            return Base64.encodeToString(buffer.array(), Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result){
    }
}
  • 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-14T23:39:02+00:00Added an answer on June 14, 2026 at 11:39 pm

    The solution was this! Replace the following:

    In decodeSampledBitmapFromStream function:

        private Bitmap decodeSampledBitmapFromStream(InputStream is,
                                                        int reqWidth,
                                                        int reqHeight) {
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
    
        InputStream aux = inputStreamCopy(is);
        BitmapFactory.decodeStream(aux, null, options);  // SkImageDecoder::Factory returned null
        try {
            aux.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(aux, null, options);
    }
    

    And in doInBackground method:

            @Override
        protected String doInBackground(Object... params) {
            try {
                InputStream is = ((android.app.Activity) params[1]).getContentResolver().openInputStream((Uri) params[0]);
                Bitmap bitmap = decodeSampledBitmapFromStream(is, 300, 300);
                System.gc();
                int bytes = bitmap.getWidth()*bitmap.getHeight()*2;
                ByteBuffer buffer = ByteBuffer.allocate(bytes);
                bitmap.copyPixelsToBuffer(buffer);
                return Base64.encodeToString(buffer.array(), Base64.DEFAULT);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    

    🙂

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.