Skip to content Skip to sidebar Skip to footer

Bitmap.getwidth()' On A Null Object Reference

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

Solution 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 Picassohttp://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:

DisplayMetricsmetrics=newDisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);

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

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

Post a Comment for "Bitmap.getwidth()' On A Null Object Reference"