将壁纸设置为适合位图可绘制的屏幕错误

时间:2016-11-30 10:47:28

标签: android bitmap drawable wallpaper

我试图在上面的API 23中使壁纸适合屏幕,但不知何故我得到位图drawable的错误。错误是“android.graphics.drawable.TransitionDrawable无法强制转换为android.graphics.drawable.BitmapDrawable”。 与API 22相同的代码,它工作得很完美。帮帮我。

这是我的代码。

 case R.id.action_wallpaper:


                    progressBar.setVisibility(View.VISIBLE);
                    BitmapDrawable drawable1 = (BitmapDrawable) image.getDrawable();
                    temp = drawable1.getBitmap();
                    // Drawable d = image.getDrawable();
                    String s = "image";
                    // temp = ((BitmapDrawable)d).getBitmap();
                    FileOutputStream out =
                            null;
                    try {
                        out = openFileOutput(s, Context.MODE_PRIVATE);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    temp.compress(Bitmap.CompressFormat.PNG, 90, out);

                    setWallpaper();
                    progressBar.setVisibility(View.GONE);

                    break;
 void setWallpaper() {

    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    //    Bitmap bitmap = drawable.getBitmap();
    Bitmap icon = drawable.getBitmap();


    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "wallpaper.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }

    Toast.makeText(getApplicationContext(), "Wallpaper Set",
            Toast.LENGTH_SHORT).show();
    Thread th = new Thread() {
        public void run() {
            //   temp = image.getDrawingCache();
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int height = metrics.heightPixels;
            int width = metrics.widthPixels;
            // Bitmap tempbitMap =
            // BitmapFactory.decodeResource(getResources(), R.drawable.img);
            Bitmap bitmap = Bitmap.createScaledBitmap(temp, width, height,
                    true);
            WallpaperManager wallpaperManager = WallpaperManager
                    .getInstance(WallpaperActivity.this);
            wallpaperManager.setWallpaperOffsetSteps(1, 1);
            wallpaperManager.suggestDesiredDimensions(width, height);
            try {
                wallpaperManager.setBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
            handler.post(new Runnable() {

                @Override
                public void run() {

                }
            });

0 个答案:

没有答案