[英]Saving image in Gallery
我正在截取屏幕截圖,並將其與應用程序名稱一起保存在存儲器中。 它沒有出現任何錯誤,也沒有將照片保存在存儲中,但是在重新啟動手機后,它會出現在圖庫中。
我的照片保存方法如下:
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();
}
}
我在此缺少什么?
謝謝
您只需要在MediaScanner上告訴您文件已存在。
像這樣:
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();
}
}
您應該在新文件上調用媒體掃描儀。
在android中使用媒體掃描器,只需添加以下代碼即可。
MediaScannerConnection.scanFile(this, new String[] { imagefile.toString() }, null, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.