Issue
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)
- The good url: https://images.unsplash.com/photo-1487239954692-e6a970698056?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=a98f159f3fb718fe2f5a24703ee75930
- The bad url: https://images.unsplash.com/photo-1487221967119-451b79e10235?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=bd41a3caa8886f9b150915f7cfa002e8
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!
Solution
(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
Answered By - halfer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.