Bitmap.getWidth()' on a null object reference

坚强是说给别人听的谎言 提交于 2019-12-24 09:07:49

问题


I just got into this problem on the line CreateScaledBitmap, I am trying to set this image as device's wallpaper and I need to scale this image to the device, thats why I am doing this method but unfortunately I cant fix this Bitmap width() error

            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:


You have Url of an image from your Firebase but approach you use to get Bitmap from Url is not efficient and probably not possible. Simple thing you need to do is to use some custom library for downloading images for example Picasso http://square.github.io/picasso/

Add to your app gradle: compile 'com.squareup.picasso:picasso:2.5.2'

And now you can use Picasso to download image from Url and convert to 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);
         }
    });


来源:https://stackoverflow.com/questions/48551226/bitmap-getwidth-on-a-null-object-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!