1.場景
??在android開發(fā)中保存應(yīng)用的圖片并插入到系統(tǒng)圖庫同時(shí)通知相冊(cè)刷新的功能,做完后發(fā)現(xiàn)在部分手機(jī)上出現(xiàn)雖然圖片保存成功了,但是相冊(cè)卻找不到圖片的問題,查找文件夾圖片也已經(jīng)存在,可就是在相冊(cè)里刷新不出來。
2.思路
2.1.保存圖片的方法
public static File saveImage(Bitmap bmp) {
File appDir = new File(Environment.getExternalStorageDirectory(), "zzs");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
??以上代碼便是將Bitmap保存圖片到指定的路徑/sdcard/Boohee/下,文件名以當(dāng)前系統(tǒng)時(shí)間命名,但是這種方法保存的圖片沒有加入到系統(tǒng)圖庫中
2.2.調(diào)用系統(tǒng)提供的插入圖庫的方法
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
??調(diào)用以上系統(tǒng)自帶的方法會(huì)把bitmap對(duì)象保存到系統(tǒng)圖庫中,但是這種方法無法指定保存的路徑和名稱,上述方法的title、description參數(shù)只是插入數(shù)據(jù)庫中的字段,真實(shí)的圖片名稱系統(tǒng)會(huì)自動(dòng)分配??此粕鲜鲞@種方法就是我們要用到的方法,但是可惜的調(diào)用上述放法插入圖庫的方法圖片并沒有立刻顯示在圖庫中,而我們需要立刻更新系統(tǒng)圖庫以便讓用戶可以立刻查看到這張圖片。
2.3.調(diào)用系統(tǒng)提供的插入圖庫的方法
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
??上面那條廣播是掃描整個(gè)sd卡的廣播,如果你sd卡里面東西很多會(huì)掃描很久,在掃描當(dāng)中我們是不能訪問sd卡,所以這樣子用戶體現(xiàn)很不好,所以下面我們還有如下的方法:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););
??或者還有如下方法:
final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
msc.scanFile("/sdcard/Boohee/image.jpg", "image/jpeg");
}
public void onScanCompleted(String path, Uri uri) {
Log.v(TAG, "scan completed");
msc.disconnect();
}
});
上面代碼的圖片路徑不管是通過自己寫方法還是系統(tǒng)插入圖庫的方法都可以很容易的獲取到。
2.4.終極完美解決方案
??如果我想把圖片保存到指定的文件夾,同時(shí)又需要圖片出現(xiàn)在圖庫里呢?sdk還提供了這樣一個(gè)方法:
MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");
??上述方法的第二個(gè)參數(shù)是image path,這樣的話就有思路了,首先自己寫方法把圖片指定到指定的文件夾,然后調(diào)用上述方法把剛保存的圖片路徑傳入進(jìn)去,最后通知圖庫更新。
3.解決辦法
(1)創(chuàng)建文件路徑可選擇Environment.getExternalStorageDirectory(),也就是(/storage/emulated/0/com.xx.xxx.xxx/),之前有問題的版本使用的是context.getExternalFilesDir(null)也就是(/storage/sdcard/Android/data/com.xxx.xxx/),部分手機(jī)相冊(cè)無法找到此路徑或者沒有權(quán)限
// 首先保存圖片
File appDir;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//android11以后
appDir = getExternalFilesDir(null);
}else {
appDir = new File(Environment.getExternalStorageDirectory(), "zzs");
}
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
(2)保存的方法添加寫入的動(dòng)態(tài)權(quán)限,把文件插入到系統(tǒng)圖庫文章來源:http://www.zghlxwxcb.cn/news/detail-821864.html
//把文件插入到系統(tǒng)圖庫
try {
MediaStore.Images.Media.insertImage(this.getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
(3) 通知圖庫更新,使用MediaStore插入到系統(tǒng)相冊(cè),使用廣播Intent.ACTION_MEDIA_SCANNER_SCAN_FILE通知相冊(cè)刷新文章來源地址http://www.zghlxwxcb.cn/news/detail-821864.html
// 通知圖庫更新
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String path = file.getAbsolutePath();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(new File(path));
intent.setData(uri);
sendBroadcast(intent);
} else {
String relationDir = file.getParent();
File file1 = new File(relationDir);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.fromFile(file1.getAbsoluteFile())));
}
完整代碼
public void saveImageToGallery(Context context, Bitmap bmp) {
// 首先保存圖片
File appDir;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//android11以后
appDir = getExternalFilesDir(null);
}else {
appDir = new File(Environment.getExternalStorageDirectory(), "zzs");
}
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//把文件插入到系統(tǒng)圖庫
try {
MediaStore.Images.Media.insertImage(this.getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 通知圖庫更新
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String path = file.getAbsolutePath();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(new File(path));
intent.setData(uri);
sendBroadcast(intent);
} else {
String relationDir = file.getParent();
File file1 = new File(relationDir);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.fromFile(file1.getAbsoluteFile())));
}
}
到了這里,關(guān)于Android保存圖片到系統(tǒng)圖庫并通知系統(tǒng)相冊(cè)刷新的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!