Android - Resizing a bitmap from InputStream returns null

486
March 01, 2017, at 4:49 PM

I am trying to get the height of the inputStream (from a url) and resize it a smaller size. I have followed Google's Android Documentation on Bitmap Resizing to accomplish this. However, I am still getting null from the output. Here, I am using an AsycnTask to load the image in background, resize it according to screen size and then return the Bitmap image. I am almost sure almost all of my input images will be high resolution and above 4096 x 4096 pixels.

Update:

I have used two input stream and connection (CommonsWare's hint, I have not dig into it yet!) and It worked to download the lower resolution pics. > 4k however, I am still getting the Too large processing error for larger images.

Here is the code I have so far....

@Override
protected Void doInBackground(String... params) {
    Bitmap output = null;
    InputStream inputStream = null;
    try {
        URL url = new URL(params[0]);
        Log.d(TAG, "_log: output URL is: " + url.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(inputStream, null, options);
        Log.d(TAG, "_log: options.inSampleSize " + options.inSampleSize);
        Log.d(TAG, "_log: Device width " + width + " height "+height);
        Log.d(TAG, "_log: options width " + options.outWidth + " height "+options.outHeight);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, width, height);
        /*---------------------------------------------------------------*/
        /*---------------------------------------------------------------*/
        /*---------------------------------------------------------------*/
        HttpURLConnection secondaryConnection = (HttpURLConnection) url.openConnection();
        secondaryConnection.connect();
        InputStream secondaryStream = secondaryConnection.getInputStream();
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        output = BitmapFactory.decodeStream(secondaryStream, null, options);
        Log.d(TAG, "_log: output size " + output.getByteCount());
        inputStream.close();
        secondaryStream.close();
        connection.disconnect();
        secondaryConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        Log.d(TAG, "_log: output with MalformedURLException " + e.toString());
    } catch (IOException e) {
        Log.d(TAG, "_log: output with IOException " + e.toString());
        e.printStackTrace();
    } catch (Exception e) {
        Log.d(TAG, "_log: output with Exception " + e.toString());
        e.printStackTrace();
    } finally {
        if (output != null) util.setupWallpaper(context, output);
        else Log.d(TAG, "_log: output is null");
    }
    return null;
} 

calculateInSampleSize is a function to load a scaled down version into memory. It's and all the codes above is directly copied from the link above.

public int calculateInSampleSize(BitmapFactory.Options options,
                                  int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

Here is my list of errors: with updates

D/skia: too Large Progressive Image (1, 5184 x 3456)> limit(16777216)
D/skia: --- decoder->decode returned false
D/RetrieveFeed: _log: output with Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getByteCount()' on a null object reference
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getByteCount()' on a null object reference

Error marked with D/Skia is from the Bitmap class. It's clear that, I have got the correct information about the image I am about to load however, somehow, I cannot resize it and I have googled and tried lots of others version. But I cannot pin point my exact error. So, any help would be highly appreciated.

PS: I have tried with Glide too. Glide's override method probably don't work with > 4096px either or I don't the work around. I have tried Picasso too. I have got the same error.

 too Large Progressive Image (1, 5184 x 3456)> limit(16777216) 

Thanks.

Rent Charter Buses Company
READ ALSO
How to find a button on the screen and get coordinates? [on hold]

How to find a button on the screen and get coordinates? [on hold]

I want to click some button on my desktop, but first I need to find itI think I should make a screenshot of my desktop, try to resolve image of my button, get coordinates of this button and only then - make a click

327
How do I iterate over a Map<Integer, Set>?

How do I iterate over a Map<Integer, Set>?

So if i have the following:

305
Dump leak connections in WildFly

Dump leak connections in WildFly

I am trying to dump leaked connections managed by a MySQL Data Source in WildFly 101, but I am not able to find the leak file or the leak dump

806
How to deploy an Dockerized Java/Widlfly Swarm Fat Jar Microservice + Database

How to deploy an Dockerized Java/Widlfly Swarm Fat Jar Microservice + Database

I am very new to Docker and I want to have both my JEE(Wildfly Swarm) Application and my PostgreSQL database dockerizedIt could be bundled in the same container or separated because honestly I don't know what would be better at this point

290