[英]Saving image in Gallery
I am taking a screenshot and saving it in storage with my application name. 我正在截取屏幕截图,并将其与应用程序名称一起保存在存储器中。 It's not giving any error and saving photo in storage, but it appears in the gallery after I restart my phone.
它没有出现任何错误,也没有将照片保存在存储中,但是在重新启动手机后,它会出现在图库中。
My save method for photo is like below: 我的照片保存方法如下:
public void saveQuoteImage(Bitmap quoteImage){
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String dirPath=Environment.getExternalStorageDirectory().toString()+File.separator+"quotes_king";
File dirFile=new File(dirPath);
if(!dirFile.exists())dirFile.mkdirs();
// String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
File imageFile = new File(dirFile.getPath()+File.separator+ now + ".jpg");
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
quoteImage.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
What I am missing in this ? 我在此缺少什么?
Thanks 谢谢
You just have to tell at MediaScanner that your file exists. 您只需要在MediaScanner上告诉您文件已存在。
Something like this: 像这样:
public final void addManually(File file) {
try{
MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
file.getAbsolutePath(), file.getName(), null);
mContext.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(file))
);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
You should invoke the media scanner on the new file. 您应该在新文件上调用媒体扫描仪。
在android中使用媒体扫描器,只需添加以下代码即可。
MediaScannerConnection.scanFile(this, new String[] { imagefile.toString() }, null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.