NullPointerException при сохранении imageView в файл

Рейтинг: 0Ответов: 0Опубликовано: 19.04.2023

В манифесте разрешения есть. Картинка с помощью Picasso загружается, но при сохранении всегда пустой файл:

             ImageView ViewPicture  = findViewById(R.id.imageView1);
            ViewPicture.setDrawingCacheEnabled(true);
            ViewPicture.buildDrawingCache(true);
            Picasso.get()
                    .load("https://apod.nasa.gov/apod/image/vir70_skyview.gif")
                    .into((ImageView) ViewPicture,            new com.squareup.picasso.Callback() {
                @Override
                public void onSuccess() {
                    //do smth when picture is loaded successfully
                    bitmap=ViewPicture.getDrawingCache();
                    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "savedBitmapSAVE.png");
                    try {
                        FileOutputStream fos = null;
                        try {
                            fos = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
                            fos.flush();
                            fos.close();
                        }
                        finally {
                            if (fos != null) fos.close();
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    };
                }
                @Override
                public void onError(Exception e) {
                    //do smth when there is picture loading error
                             }
            }

);

Ошибка:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

Ответы

Ответов пока нет.