save image in gallery android

ぐ巨炮叔叔 提交于 2019-12-03 15:38:09
android
public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                 Log.i("ExternalStorage", "Scanned " + path + ":");
                 Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
}

Worked for me. Thanks for your time

Just you are code is missing the MediaScannerConnection class. This class scans for new files and directories in gallery that are created from your app. See a full demo example demonstrating this. http://whats-online.info/science-and-tutorials/135/how-to-save-an-image-to-gallery-in-android-programmatically/

Just change these lines, it will work -

File filepath = Environment.getExternalStorageDirectory().toString();
        File dir = new File(filepath + "/folder name/");
        dir.mkdirs();
        File file = new File(dir, image + ".png");

If you want to save an Image inside a directory then this Code worked for me!

saveImage(data);

            private void saveImage(Bitmap data) {
                File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
                if(!createFolder.exists())
                createFolder.mkdir();
                File saveImage = new File(createFolder,"downloadimage.jpg");
                try {
                    OutputStream outputStream = new FileOutputStream(saveImage);
                    data.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

This Worked for me.

Add MediaScannerConnection to scan the file. Specify the file url and mimeType

MediaScannerConnection.scanFile(context,new String[] { image.getAbsolutePath() }, 
new String[] {"images/*"}, new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ScanCompleted", "Scanned " + path + ":");
    }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!