Save Bitmap in Android as JPEG in External Storage

2019-02-04 09:15发布

I am using this code to save Bitmap in External Storage but it does not create the folder if it not exists:

String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOutputStream = null;
        File file = new File(path + "/Captures/", "screen.jpg");
        try {
            fOutputStream = new FileOutputStream(file);

            capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

            fOutputStream.flush();
            fOutputStream.close();

            MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        }

How can I save the image in the new directory if not exists and save default if the folder is there in the device?

5条回答
你好瞎i
2楼-- · 2019-02-04 09:52

try this it will gives u result sure:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
    file.delete();
try {
    FileOutputStream out = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

add this one to show in gallery:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

look at this link for clear answer: show folder images in gallery

查看更多
乱世女痞
3楼-- · 2019-02-04 09:53

You should take a look in the documentation of File, you'll find the method mkdir(). It is pretty much the same as the unix one : https://developer.android.com/reference/java/io/File.html#mkdir()

查看更多
Explosion°爆炸
4楼-- · 2019-02-04 09:54

Please use the below code snippet might be help full

  String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOutputStream = null;
    File file = new File(path + "/Captures/", "screen.jpg");
    if (!file.exists()) {
        file.mkdirs();
    }

    try {
        fOutputStream = new FileOutputStream(file);

        capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

        fOutputStream.flush();
        fOutputStream.close();

        MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
        return;
    }
查看更多
迷人小祖宗
5楼-- · 2019-02-04 09:57

Use the following:

File dir = new File(path + "/Captures/");
if(!dir.exists()) {
    dir.mkdirs();
}
File file = new File(path + "/Captures/", "screen.jpg");
 ......
查看更多
家丑人穷心不美
6楼-- · 2019-02-04 10:14

late but might be helpful to someone. use below code it will save the bitmap in external directory more faster because of BufferOutPutStream.

    public boolean storeImage(Bitmap imageData, String filename) {
            // get path to external storage (SD card)

            File sdIconStorageDir = null;


            sdIconStorageDir = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/myAppDir/");
            // create storage directories, if they don't exist
if(!sdIconStorageDir.exist())
{
sdIconStorageDir.mkdirs();
}            
            try {
                String filePath = sdIconStorageDir.toString() + File.separator + filename;
                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
                //Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show();
                // choose another format if PNG doesn't suit you
                imageData.compress(Bitmap.CompressFormat.PNG, 100, bos);
                bos.flush();
                bos.close();


            } catch (FileNotFoundException e) {
                Log.w("TAG", "Error saving image file: " + e.getMessage());
                return false;
            } catch (IOException e) {
                Log.w("TAG", "Error saving image file: " + e.getMessage());
                return false;
            }
            return true;
        }
查看更多
登录 后发表回答