1

I am working on a wallpaper app. I have written a AsyncTask to download a photo from selected url and load it as Wallpaper (All in background). Now, The AsyncTask functions sometimes. And I, myself can't get to understand what's the reason behind not being able to load/download the image (As bitmap) from the URL. Both url (the good one and the bad one) is working and loading the correct image on the browser.

My AsyncTask class:

@Override
protected Void doInBackground(String... params) {
    Bitmap output = null;
    try {
        URL url = new URL(params[0]);
        Log.d(TAG, "_log: output URL is: "+url.toString());

        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        output = BitmapFactory.decodeStream(inputStream);

        Log.d(TAG, "_log: output size "+output.getByteCount());
    } 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;
}

and the setupWallpaper function is:

public void setupWallpaper(Context context, Bitmap image) {
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
    try {
        if (image == null) {
            makeToast(context, "Image is null!");
        } else {
            wallpaperManager.setBitmap(image);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And the error is just a stackTrace caught in exception while I tried to get the size of a null object, output.

output with Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getByteCount()' on a null object reference

RetrieveFeed.doInBackground(RetrieveFeed.java:57)
RetrieveFeed.doInBackground(RetrieveFeed.java:27)

Permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

I am not trying to load this Image into any imageView, I am sending this Image into WallPaperManager to load as Wallpaper.

I want to point out that I tried the same thing with Glide and same result appeared!

halfer
  • 19,824
  • 17
  • 99
  • 186
O_o
  • 1,103
  • 11
  • 36
  • little remark - You don't need to set setDoInput(true) as this value is true by default – j2ko Feb 24 '17 at 15:10
  • Did you load those URLs independent or in sequence. Did you try to change the order ? – j2ko Feb 24 '17 at 15:13
  • @j2ko, thanks about that. And there is no particular order of sequence. The good urls always load the photo and bad url never loads the photo. – O_o Feb 24 '17 at 15:16
  • The strange thing here is that there are no exceptions at all during loading and decoding... – j2ko Feb 24 '17 at 15:37
  • @j2ko, I had tried with glide as well, however, they still provided the same result. Does the size of the image matters here? These are raw image files. Both of them are >5MB – O_o Feb 24 '17 at 15:39
  • @j2ko, found the error. Too large image. too Large Progressive Image (1, 5184 x 3456)> limit(16777216), decoder->decode returned false :P – O_o Feb 24 '17 at 15:44
  • 2
    Then you need to combine your solution with following https://developer.android.com/topic/performance/graphics/load-bitmap.html – j2ko Feb 24 '17 at 15:47
  • Why don`t you try [Picaso](http://square.github.io/picasso/) Instead doing it whit AsyncTask? – MeLean Feb 24 '17 at 15:11
  • Does Picasso load image's as Bitmap to WallpaperManager class. Cause, I am not loading the image in any imageView, I am directly throwing it to WallPaperManager as bitmap to set as wallpaper. – O_o Feb 24 '17 at 15:18
  • I am downvoting as it seems I have removed [solved] home-made tags from your titles before, and I don't want to have to keep clearing up after you. Please do not make these title amendments here - we have an acceptance system for that. – halfer Feb 27 '17 at 13:05
  • @halfer, Thanks for crawling through my profile and amending multiples of my question. In the matter of that [Solved] tag, well that's because when I google it around, I usually check the answer that has a Solve mark in front of it. It's pretty much saves the time after half an hour or one hour of google searching through in complete answers! I get it, hardly any answer/question will fit my criteria/working procedure however, you see that little 8 characters in front of a question, drags me to that posts. However, thank you for making stackoverflow clearer and cleaner. – O_o Feb 27 '17 at 13:24
  • @O_o Any idea on how can we convert such images into smaller ones? – Vadim Kotov Sep 25 '19 at 08:31
  • 1
    @VadimKotov, Sorry man. I have not been on android development since 2016. But I think you should not convert images on the mobile end. Backend developer must either compress the image or give you two or three URL like HD, 720 and 480, so you can load whichever you want. Trying to compress images on a mobile end, will put pressure on devices and you will get unpredictable results in different device types. – O_o Sep 25 '19 at 08:56
  • @O_o I'm trying to resize images before uploading, because I'd like to avoid uploading big chunks of data via mobile connection. Anyway, thanks for the idea! – Vadim Kotov Sep 25 '19 at 08:59
  • @VadimKotov, when I was working, if image size crosses 16777216 in byte count, then it Bitmap won't be able to process it on mobile end. – O_o Sep 25 '19 at 08:59

2 Answers2

2

(Posted on behalf of the OP).

The problem was, the bad url images are too large to process, so Bitmap fails silently.

D/skia: --- too Large Progressive Image (1, 5184 x 3456)> limit(16777216)
D/skia: --- decoder->decode returned false
halfer
  • 19,824
  • 17
  • 99
  • 186
1

If I were you, I'd use Picasso Library to handle all of this

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

This line of code download the image from URL you specify and load it in imageView.

But you need to compile it first in build.gradle.
For more details visit library site.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Does Picasso load image's as Bitmap to WallpaperManager class. Cause, I am not loading the image in any imageView, I am directly throwing it to WallPaperManager as bitmap to set as wallpaper. – O_o Feb 24 '17 at 15:18
  • 1
    you can visit this if you are interested you find it in [second answer][1] [1]: http://stackoverflow.com/questions/20181491/use-picasso-to-get-a-callback-with-a-bitmap –  Feb 24 '17 at 15:23