空对象引用

时间:2018-01-31 20:53:10

标签: android bitmap android-bitmap bitmapimage

我刚刚在CreateScaledBitmap一行遇到这个问题,我试图将此图像设置为设备的壁纸,我需要将此图像缩放到设备,这就是我为什么要这样做但不幸的是我无法修复此Bitmap宽度()错误

            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) {

                }
            });

1 个答案:

答案 0 :(得分:1)

您拥有来自Url的{​​{1}}图片,但您使用Firebase获取Bitmap的方法效率不高,可能无法实现。您需要做的很简单的事情是使用一些自定义Url来下载图片,例如 Picasso http://square.github.io/picasso/

添加到您的libraryapp gradle

现在,您可以使用compile 'com.squareup.picasso:picasso:2.5.2'Picasso下载图片并转换为Url

Bitmap