我正在尝试找到一种方法(与android kitkat和下一代兼容)在SD卡上写照片并让它们对画廊应用程序可见。
我试图直接在/ storage / externalSdCard / DCIM /上写,但当然我不能,因为我正在运行kitkat。
好的,我可以写在里面,照片在SD卡上。但我在Gallery应用程序中看不到它
首先,当你写完文件后,在你的flush()
上调用getFD().sync()
,然后是close()
,然后是FileOutputStream
。
然后,使用MediaScannerConnection
及其scanFile()
方法获取由MediaStore
索引的新编写的文件。
void saveImage() {
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();
}
}