Android 4.4.4及更低版本上的ImageView.getDrawingCache()null

时间:2015-08-29 23:39:06

标签: android

我尝试从Imageview设置设备壁纸。我的代码在运行Android 5.0及更高版本的设备上完美运行但由于某些原因,它不能在4.4.4或更低版本上运行。

我从菜单项设置壁纸。这是代码:

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();
    }

}

这里是logcat输出:

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)

代码来自here.

我不明白为什么它适用于Android 5.0及以上版本,但不在其中。

帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

先获取Drawable

Drawable d = wallpaper_img_menu.getDrawable();

然后在您喜欢的任何Drawable上绘制Canvas,确保在调用d.setBounds(...)

之前设置界限(d.draw(canvas)