1

I'm attempting to set the device wallpaper from an Imageview. My code works perfectly on devices running Android 5.0 and up but for some reason, it doesn't work on 4.4.4 or below.

I'm setting the wallpaper from a menu item. Here is the code:

else if (id == R.id.action_quick_set) {

    wallpaper_img_menu = (ImageView) findViewById(R.id.wallpaper_image);

    wallpaper_img_menu.setDrawingCacheEnabled(true);

    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null
    wallpaper_img_menu.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    wallpaper_img_menu.layout(0, 0,
                wallpaper_img_menu.getMeasuredWidth(),
                wallpaper_img_menu.getMeasuredHeight());

    wallpaper_img_menu.buildDrawingCache(true);
    // This is what's null (line 807); everything else works fine
    Bitmap b = Bitmap.createBitmap(wallpaper_img_menu.getDrawingCache());
    wallpaper_img_menu.setDrawingCacheEnabled(false); // clear drawing cache

    try {
        WallpaperManager.getInstance(getBaseContext()).setBitmap(b);
        Toast.makeText(getBaseContext(), "Done!", Toast.LENGTH_SHORT).show();
        finish();
    } catch (IOException | NullPointerException e) {
        e.printStackTrace();
    }

}

Here's the logcat output:

08-29 19:34:57.025  25668-25668/com.hidden.hidden E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at android.graphics.Bitmap.createBitmap(Bitmap.java:505)
            at com.hidden.hidden.viewer.WallpaperViewer.onOptionsItemSelected(WallpaperViewer.java:807)
            at android.app.Activity.onMenuItemSelected(Activity.java:2566)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:325)
            at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
            at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
            at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:583)
            at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
            at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
            at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
            at android.widget.AdapterView.performItemClick(AdapterView.java:298)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
            at android.widget.AbsListView$1.run(AbsListView.java:3463)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

The code is from here.

What I don't understand is why it works for Android 5.0 and up but nothing below.

Help appreciated!

Community
  • 1
  • 1
Andrew Quebe
  • 2,263
  • 5
  • 25
  • 53

1 Answers1

2

get the Drawable first:

Drawable d = wallpaper_img_menu.getDrawable();

and then draw your Drawable on any Canvas you like, make sure you set the bounds (d.setBounds(...)) before calling d.draw(canvas)

pskink
  • 23,874
  • 6
  • 66
  • 77