后保存的图像不能看到它在Android的画廊 [英] After saved image can't see it in Gallery android
问题描述
我保存图像从 RES /绘制
到图库
使用的InputStream
和的OutputStream
。它工作正常,并保存图像。但问题是,它不会在画廊更新映像。如果我使用支票夹ES文件浏览器
然后我可以在那里看到的图像。我还检查 DDMS
,它也只要写有更新的图像
code会执行。结果< BR>它工作正常,如果我救服务器映像。如何prevent这个问题?我想,只要保存图像更新库。搜索结果我也试过 MediaScanner
来扫描文件夹,但没有效果。搜索结果
我的code:
Toast.makeText(背景下,下载图片... \\ n请等待。
Toast.LENGTH_LONG).show(); 文件直接=新的文件(Environment.getExternalStorageDirectory()
+/图片); 如果(!direct.exists()){
direct.mkdirs();
} 日期格式DATEFORMAT =新的SimpleDateFormat(DDMMYYYY-HHMMSS);
日期日期=新的日期();
字符串CurrentDateTime = dateFormat.format(日期); 输入的InputStream = NULL;
OutputStream的输出= NULL; 尝试{
输入= context.getResources()。openRawResource(
context.getResources()则getIdentifier(
@绘制/+ picName,绘制
context.getPackageName()));
输出=新的FileOutputStream(直接+/+IMG-
+ CurrentDateTime +.JPG); 字节[] buf中=新的字节[1024];
INT LEN;
而((LEN = input.read(BUF))大于0){
output.write(BUF,0,LEN);
} MediaScannerConnection.scanFile(背景下,
新的String [] {direct.toString()},空,
新MediaScannerConnection.OnScanCompletedListener(){
公共无效onScanCompleted(字符串路径,开放的URI){
Log.i(ExternalStorage,扫描+路径+:);
Log.i(ExternalStorage, - &GT; URI =+ URI);
}
}); Toast.makeText(背景下,图像保存。Toast.LENGTH_LONG).show();
}赶上(例外五){
Log.e(内部图像存储错误 - &gt;中,e.toString()); Toast.makeText(背景下,
无法保存影像\\ n错误:+ e.toString()+
Toast.LENGTH_LONG).show();
} {最后
尝试{
如果(输入!= NULL){
input.close();
}
如果(输出!= NULL){
output.close();
}
}赶上(IOException异常忽略){
Log.e(内部图像存储错误 - &gt;中,ignored.toString()); Toast.makeText(
的背景下,
无法保存影像\\ n错误:+ ignored.toString()
+,Toast.LENGTH_LONG).show();
}
}
您必须广播外部目录...
sendBroadcast(新意图(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse(文件://+ Environment.getExternalStorageDirectory())));
如果您在SD卡创建外部文件夹,然后它不是在GALLARY显示器,然后使用下面code。
如果(android.os.Build.VERSION.SDK_INT&GT; = android.os.Build.VERSION_ codeS.KITKAT){
调用Runtime.getRuntime()。EXEC(
AM广播-a android.intent.action.MEDIA_MOUNTED -d文件://
+ CommonVariable.abc_FOLDER);
}其他{
sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse(文件://+ CommonVariable.abc_FOLDER)));
}
扫描等方法。
乌里contentUri = Uri.fromFile(destFile);
意图mediaScanIntent =新意图(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
我还希望对你有用。
I am saving Image from res/drawable
to Gallery
using InputStream
and OutputStream
. It works fine and save image. But issue is that It does not update Image in Gallery. If I check folder using ES File Explorer
then I can see image there. I also checked ddms
, It also update image there as soon as write
code executes.
It works fine if I save Server Image. How to prevent this issue ? I want to update Gallery as soon as Image save.
I also tried MediaScanner
to scan folder but no effect.
My Code:
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();
}
}
you must broadcast external directory...
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
if you create external folder in sdcard and then its not display in gallary then use below code.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
Runtime.getRuntime().exec(
"am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://"
+ CommonVariable.abc_FOLDER);
} else {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + CommonVariable.abc_FOLDER)));
}
other method for scanning.
Uri contentUri = Uri.fromFile(destFile);
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
i hope its useful to you.
这篇关于后保存的图像不能看到它在Android的画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!