O_o
O_o

Reputation: 1103

Unable to load photos from URL in Android

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!

Upvotes: 1

Views: 169

Answers (2)

user6672223
user6672223

Reputation:

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.

Upvotes: 1

halfer
halfer

Reputation: 20440

(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

Upvotes: 2

Related Questions