我刚刚在CreateScaledBitmap行上遇到了这个问题,我试图将此图像设置为设备的墙纸,并且需要将该图像缩放到设备上,这就是为什么我要使用此方法,但是不幸的是我无法修复此Bitmap width()错误

            setWall.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(View view) {

            Picasso.with(getApplicationContext()).load(imageBrought).into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                    DisplayMetrics metrics = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metrics);

                    int height = metrics.heightPixels;
                    int width = metrics.widthPixels;


                    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);

                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(AppMomentSelected.this);
                    wallpaperManager.setWallpaperOffsetSteps(1, 1);
                    wallpaperManager.suggestDesiredDimensions(width, height);


                    try {

                        wallpaperManager.setBitmap(bitmap);

                    } catch (IOException e) {

                        e.printStackTrace();
                    }

                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });

最佳答案

您已经从Url中获取图像的Firebase,但是用于从Bitmap获取Url的方法效率不高,而且可能无法实现。您需要做的简单事情是使用一些自定义library来下载图像,例如毕加索
http://square.github.io/picasso/

添加到您的app gradlecompile 'com.squareup.picasso:picasso:2.5.2'

现在,您可以使用PicassoUrl下载图像并转换为Bitmap

                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);

                int height = metrics.heightPixels;
                int width = metrics.widthPixels;
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(AppMomentSelected.this);
                wallpaperManager.setWallpaperOffsetSteps(1, 1);
                wallpaperManager.suggestDesiredDimensions(width, height);

                Picasso.with(this)
                .load(imageBrought)
                .resize(width, height)
                .into(new Target() {
                 @Override
                  public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from) {
                 /* Save the bitmap or do something with it here */
                 wallpaperManager.setBitmap(bitmap);
         }
    });

关于android - 在空对象引用上的Bitmap.getWidth()',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48551226/

10-09 07:05
查看更多