天天看點

android 儲存網絡圖檔到SD卡方法

android 儲存網絡圖檔到SD卡方法

public static boolean saveBitmap(String path, String name, Bitmap bitmap, Context context) {
		boolean ret = true;
		FileOutputStream fOut = null;
		try {
			File f = new File(path + File.separator + name);
			f.deleteOnExit();
			f.createNewFile();
			fOut = new FileOutputStream(f);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

			<span style="color:#ff0000;">Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
			scanIntent.setData(Uri.fromFile(f));
			context.sendBroadcast(scanIntent);</span>
		} catch (IOException e) {
			ret = false;
			Log.e("saveBitmap error ", e);
		} finally {
			if (fOut != null) {
				try {
					fOut.close();
				} catch (IOException e) {
					e.printStackTrace();
					FansLog.v(e.toString());
				}
			}
		}


		return ret;
	}
           

代碼如上,其中紅色部分為通知android系統掃描特定目錄/檔案。如果不加如該代碼,手機圖庫不會感覺到該圖檔,除非手機下次啟動時自動掃描所有檔案。