可能大家都知道我們儲存相簿到Android手機的時候,然後去開啟系統圖庫找不到我們想要的那張圖片,那是因為我們插入的圖片還沒有更新的緣故,先講解下插入系統圖庫的方法吧,很簡單,一句代碼就能實現
MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");
通過上面的那句代碼就能插入到系統圖庫,這時候有一個問題,就是我們不能指定插入照片的名字,而是系統給了我們一個目前時間的毫秒數為名字,有一個問題鬱悶了很久,我還是先把insertImage的源碼貼出來吧
/** * Insert an image and create a thumbnail for it. * * @param cr The content resolver to use * @param source The stream to use for the image * @param title The name of the image * @param description The description of the image * @return The URL to the newly created image, or <code>null</code> if the image failed to be stored * for any reason. */ public static final String insertImage(ContentResolver cr, Bitmap source, String title, String description) { ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, title); values.put(Images.Media.DESCRIPTION, description); values.put(Images.Media.MIME_TYPE, "image/jpeg"); Uri url = null; String stringUrl = null; /* value to be returned */ try { url = cr.insert(EXTERNAL_CONTENT_URI, values); if (source != null) { OutputStream imageOut = cr.openOutputStream(url); try { source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut); } finally { imageOut.close(); } long id = ContentUris.parseId(url); // Wait until MINI_KIND thumbnail is generated. Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null); // This is for backward compatibility. Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F, Images.Thumbnails.MICRO_KIND); } else { Log.e(TAG, "Failed to create thumbnail, removing original"); cr.delete(url, null, null); url = null; } } catch (Exception e) { Log.e(TAG, "Failed to insert image", e); if (url != null) { cr.delete(url, null, null); url = null; } } if (url != null) { stringUrl = url.toString(); } return stringUrl; }
上面方法裡面有一個title,我剛以為是可以設定圖片的名字,設定一下,原來不是,鬱悶,哪位高手知道title這個欄位是幹嘛的,告訴下小弟,不勝感激!
當然Android還提供了一個插入系統相簿的方法,可以指定儲存圖片的名字,我把源碼貼出來吧
/** * Insert an image and create a thumbnail for it. * * @param cr The content resolver to use * @param imagePath The path to the image to insert * @param name The name of the image * @param description The description of the image * @return The URL to the newly created image * @throws FileNotFoundException */ public static final String insertImage(ContentResolver cr, String imagePath, String name, String description) throws FileNotFoundException { // Check if file exists with a FileInputStream FileInputStream stream = new FileInputStream(imagePath); try { Bitmap bm = BitmapFactory.decodeFile(imagePath); String ret = insertImage(cr, bm, name, description); bm.recycle(); return ret; } finally { try { stream.close(); } catch (IOException e) { } } }
啊啊,貼完源碼我才發現,這個方法調用了第一個方法,這個name就是上面方法的title,暈死,這下更加鬱悶了,反正我設定title無效果,求高手為小弟解答,先不管了,我們繼續往下說
上面那段代碼插入到系統相簿之後還需要發條廣播
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
上面那條廣播是掃描整個sd卡的廣播,如果你sd卡裡面東西很多會掃描很久,在掃描當中我們是不能訪問sd卡,所以這樣子使用者體現很不好,用過的朋友都知道,儲存圖片到系統相簿並沒有掃描整個SD卡,所以我們用到下面的方法
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File("/sdcard/image.jpg")); intent.setData(uri); mContext.sendBroadcast(intent);
或者用MediaScannerConnection
final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() { public void onMediaScannerConnected() { msc.scanFile("/sdcard/image.jpg", "image/jpeg"); } public void onScanCompleted(String path, Uri uri) { Log.v(TAG, "scan completed"); msc.disconnect(); } });
也行你會問我,怎麼擷取到我們剛剛插入的圖片的路徑?呵呵,這個自有方法擷取,insertImage(ContentResolver cr, Bitmap source,String title, String description),這個方法給我們返回的就是插入圖片的Uri,我們根據這個Uri就能擷取到圖片的絕對路徑
private String getFilePathByContentResolver(Context context, Uri uri) {if (null == uri) {return null;} Cursor c = context.getContentResolver().query(uri, null, null, null, null); String filePath = null; if (null == c) { throw new IllegalArgumentException( "Query on " + uri + " returns null result."); } try { if ((c.getCount() != 1) || !c.moveToFirst()) { } else { filePath = c.getString( c.getColumnIndexOrThrow(MediaColumns.DATA)); } } finally { c.close(); } return filePath; }
根據上面的那個方法擷取到的就是圖片的絕對路徑,這樣子我們就不用發送掃描整個SD卡的廣播了,呵呵,寫到這裡就算是寫完了,寫的很亂,希望大家將就的看下,希望對你有協助!