Android儲存圖片到自定義資料夾並展示在系統圖庫
阿新 • • 發佈:2019-02-08
經過自己整理,思路來自 stormzhang 的博文
目的是將圖片儲存到自定義目錄下,並在系統圖庫中展示
一、儲存圖片到自定義路徑
通常情況下,我們對儲存圖片的處理是這樣的:
public static File saveImage(Bitmap bmp) { File appDir = new File(Environment.getExternalStorageDirectory(), "SelfDefine"); 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(); } }
這樣,圖片就被儲存到了/sdcard/SelfDefine/路徑下了
二、將圖片插入系統圖庫並更新
這樣就可以通過android系統提供的一個多媒體資料庫類:MediaStore,把bitmap圖片插入到系統圖庫中,但是無法指定儲存的路徑和名稱,引數title、description引數只是插入資料庫中的欄位,真實的圖片名稱系統會自動分配。<code class="language-ruby" data-lang="ruby"><span class="no">MediaStore</span><span class="o">.</span><span class="n">Images</span><span class="o">.</span><span class="n">Media</span><span class="o">.</span><span class="n">insertImage</span><span class="p">(</span><span class="n">getContentResolver</span><span class="p">(),</span> <span class="n">bitmap</span><span class="p">,</span> <span class="s2">"title"</span><span class="p">,</span> <span class="s2">"description"</span><span class="p">);</span></code>
MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");
此方法是可以制定目錄的,符合我們自定義的要求。
注意,以上方法不會更新相簿,需要手工再去更新相簿,才可以將圖片顯示在相簿中。
更新相簿方法如下:
1、廣播掃描SD卡,掃描中不能訪問SD卡,體驗不好
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
2、掃描指定檔案
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/SelfDefine/image.jpg"))););
3、重寫MedisScannerConnectionClient類的 onMediaScannerConnected() onScanCompleted
final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
msc.scanFile("/sdcard/SelfDefine/image.jpg", "image/jpg");
}
public void onScanCompleted(String path, Uri uri) {
Log.v(TAG, "scan completed");
msc.disconnect();
}
});
三、兩者結合的終極方法
public static void saveImageToGallery(Context context, Bitmap bmp) {
// 首先儲存圖片
File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
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();
}
// 其次把檔案插入到系統圖庫
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最後通知相簿更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
}