Skip to content Skip to sidebar Skip to footer

Android: Write Photo Files On Sd Card That Are Viewable In The Gallery

I'm trying to found a way (compatible with android kitkat and next) to write photos on the SD Card and make them visible to the gallery app. If I use Environment.getExternalStorag

Solution 1:

Ok, I can write inside and the photo is on the SDCard. But I can't see it in the Galery app

First, when you are done writing to the file, call flush(), then getFD().sync(), then close(), all on your FileOutputStream.

Then, use MediaScannerConnection and its scanFile() method to get the newly-written file indexed by the MediaStore.

Solution 2:

voidsaveImage() {
        File filename;
        try {
            String path = Environment.getExternalStorageDirectory().toString();

            new File(path + "/folder/subfolder").mkdirs();
            filename = new File(path + "/folder/subfolder/image.jpg");

            FileOutputStream out = new FileOutputStream(filename);

            bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

            MediaStore.Images.Media.insertImage(getContentResolver(), filename.getAbsolutePath(), filename.getName(), filename.getName());

            Toast.makeText(getApplicationContext(), "File is Saved in  " + filename, 1000).show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Post a Comment for "Android: Write Photo Files On Sd Card That Are Viewable In The Gallery"