1

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

                }
            });
13
  • bmap2 mustn't be null make a null checking before performing an action. Commented Jan 31, 2018 at 22:43
  • What is the error, post your log Commented Feb 1, 2018 at 10:40
  • @Rino Hello, I did an if statemant and bmap2 is null.. I dont know why.. in any case, this is the error: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nicco.wallpapersapp, PID: 11771 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:658) at com.example.nicco.wallpapersapp.AppMomentSelected$1.onClick(AppMomentSelected.java:85) Commented Feb 1, 2018 at 13:17
  • @Yupi Hi Yupi, I did an if statemant and yeah.. bmap2 is null.. I dont know why, how could I fix it? look, I updated the code in the question Commented Feb 1, 2018 at 13:21
  • What is imageBrought probably in it is problem? And can you this part change to getBytes("UTF-8"); Commented Feb 1, 2018 at 13:31

1 Answer 1

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);
         }
    });
Sign up to request clarification or add additional context in comments.

7 Comments

look the question on top, I updated it with your solution, it works but I have a question, is it sure now, watching the code on top updated thanks by you, that the image gets scaled into the device?
I think Picasso offers resizing option I will update the answer.
I tried on the Nexus 5X emulator and it works fine but on my mom's Huawei it doesnt :/ its not scaled the image, it is zoomed
You can hardcode width and height also putting the exact number
how can I know that? I mean people may have different screen size, is there a way to fit it automatically? I am searching too on the web
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.