Antoine Berger
Antoine Berger

Reputation: 85

Saving image from ImageView to storage

I followed the answers of Saving an image from ImageView into internal storage but I still can't save anything.. My code is here :

 public void buttonPickImage(View view) {

        FileOutputStream fos;

        bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

        Random rng = new Random();
        int n = rng.nextInt(1000);




        try {

            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/BAC");
            bool = dir.mkdir();

            File file = new File(dir, "BAC_"+n+".jpg");


            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            fos.flush();
            fos.close();
            Toast.makeText(getApplicationContext(),"Image sauvegardée"+bool,Toast.LENGTH_SHORT).show();

        }catch (java.io.IOException e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"IOException: " + e.getMessage(),Toast.LENGTH_SHORT).show();
        }

    }

With this method I get the IOExeception with messae : java.io.FileNotFoundException: /storage/emulated/0/BAC/BAC_396.jpg: open failed: ENOENT (No such file or directory)

I also tried this to save it to internal storage but its not working for me : https://www.tutorialspoint.com/how-to-write-an-image-file-in-internal-storage-in-android With this method, program runs but boolean mkdir gives me false.

Thanks for helping me

Upvotes: 0

Views: 1010

Answers (1)

Antoine Berger
Antoine Berger

Reputation: 85

Finally got it working using Media Store instead of getExternalStorageDirectory as

This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

MediaStore is also useful as it allows you to get the image in your android gallery app.

So my solution is :

ImageView imageView = findViewById(R.id.image);
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "any_picture_name");
        values.put(MediaStore.Images.Media.BUCKET_ID, "test");
        values.put(MediaStore.Images.Media.DESCRIPTION, "test Image taken");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        OutputStream outstream;
        try {
            outstream = getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
            outstream.close();
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
        }

Still thank you @blackapps for explaining me some basics things about the IOexception, mkdir and toasts. It'll be useful anyway.

Upvotes: 1

Related Questions