1. 程式人生 > >android儲存圖片後相簿中不能馬上顯示的解決、!

android儲存圖片後相簿中不能馬上顯示的解決、!

圖片儲存至相簿後不能馬上儲存是因為相簿不是每次都直接掃描所有目錄,而是主要在開機時才掃描,並將圖片路徑等資訊存至
相應資料庫,進入相簿時直接從資料庫讀取所有掃描到的圖片。所以直接儲存圖片至目錄,相簿不能立刻顯示出來。解決方法是,

儲存圖片後,直接把路徑等相關資訊直接插入資料庫即可。

public static boolean saveImgToGallery(String fileName) {
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判斷sd卡是否存在
		if (!sdCardExist)
			return false;

		try {
			// String url = MediaStore.Images.Media.insertImage(cr, bmp,
			// fileName,
			// "");
			// app.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
			// .parse("file://"
			// + Environment.getExternalStorageDirectory())));

			// debug
			ContentValues values = new ContentValues();
			values.put("datetaken", new Date().toString());
			values.put("mime_type", "image/png");
			values.put("_data", fileName);
			// values.put("title", this.a.getString(2131230720));
			// values.put("_display_name", (String)localObject1);
			// values.put("orientation", "");
			// values.put("_size", Integer.valueOf(0));
			Application app = MyApplication.getThis();
			ContentResolver cr = app.getContentResolver();
			cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}