我将图像从保存res/drawable
到Gallery
使用InputStream
和OutputStream
。它工作正常并保存图像。但问题是它不会更新图库中的图像。如果我使用检查文件夹,ES File Explorer
那么我可以在那里看到图像。我还检查了,它也会在代码执行ddms
后立即更新图像。如果我保存服务器图像,它工作正常。如何防止这个问题?我想在图像保存后立即更新图库。我也尝试扫描文件夹但没有效果。我的代码:write
MediaScanner
Toast.makeText(context, "Downloading Image...\nPlease Wait.",
Toast.LENGTH_LONG).show();
File direct = new File(Environment.getExternalStorageDirectory()
+ "/Images");
if (!direct.exists()) {
direct.mkdirs();
}
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy-HHmmss");
Date date = new Date();
String CurrentDateTime = dateFormat.format(date);
InputStream input = null;
OutputStream output = null;
try {
input = context.getResources().openRawResource(
context.getResources().getIdentifier(
"@drawable/" + picName, "drawable",
context.getPackageName()));
output = new FileOutputStream(direct + "/" + "IMG-"
+ CurrentDateTime + ".jpg");
byte[] buf = new byte[1024];
int len;
while ((len = input.read(buf)) > 0) {
output.write(buf, 0, len);
}
MediaScannerConnection.scanFile(context,
new String[] { direct.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Toast.makeText(context, "Image Saved.", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("Internal Image Save Error->", e.toString());
Toast.makeText(context,
"Couldn't Save Image.\nError:" + e.toString() + "",
Toast.LENGTH_LONG).show();
} finally {
try {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
} catch (IOException ignored) {
Log.e("Internal Image Save Error->", ignored.toString());
Toast.makeText(
context,
"Couldn't Save Image.\nError:" + ignored.toString()
+ "", Toast.LENGTH_LONG).show();
}
}