Android拍照保存在系統(tǒng)相冊不顯示的問題解決方法
更新時間:2013年06月03日 17:51:57 作者:
我們保存相冊到Android手機的時候,然后去打開系統(tǒng)圖庫找不到我們想要的那張圖片,那是因為我們插入的圖片還沒有更新的緣故,下面與大家分享下此問題的解決方法
可能大家都知道我們保存相冊到Android手機的時候,然后去打開系統(tǒng)圖庫找不到我們想要的那張圖片,那是因為我們插入的圖片還沒有更新的緣故,先講解下插入系統(tǒng)圖庫的方法吧,很簡單,一句代碼就能實現(xiàn)
MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");
通過上面的那句代碼就能插入到系統(tǒng)圖庫,這時候有一個問題,就是我們不能指定插入照片的名字,而是系統(tǒng)給了我們一個當(dāng)前時間的毫秒數(shù)為名字,有一個問題郁悶了很久,我還是先把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,我剛以為是可以設(shè)置圖片的名字,設(shè)置一下,原來不是,郁悶,哪位高手知道title這個字段是干嘛的,告訴下小弟,不勝感激!
當(dāng)然Android還提供了一個插入系統(tǒng)相冊的方法,可以指定保存圖片的名字,我把源碼貼出來吧
/**
* 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) {
}
}
}
啊啊,貼完源碼我才發(fā)現(xiàn),這個方法調(diào)用了第一個方法,這個name就是上面方法的title,暈死,這下更加郁悶了,反正我設(shè)置title無效果,求高手為小弟解答,先不管了,我們繼續(xù)往下說
上面那段代碼插入到系統(tǒng)相冊之后還需要發(fā)條廣播
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
上面那條廣播是掃描整個sd卡的廣播,如果你sd卡里面東西很多會掃描很久,在掃描當(dāng)中我們是不能訪問sd卡,所以這樣子用戶體現(xiàn)很不好,用過微信的朋友都知道,微信保存圖片到系統(tǒng)相冊并沒有掃描整個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,我們根據(jù)這個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;
}
根據(jù)上面的那個方法獲取到的就是圖片的絕對路徑,這樣子我們就不用發(fā)送掃描整個SD卡的廣播了,呵呵,寫到這里就算是寫完了,寫的很亂,希望大家將就的看下,希望對你有幫助!
復(fù)制代碼 代碼如下:
MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");
通過上面的那句代碼就能插入到系統(tǒng)圖庫,這時候有一個問題,就是我們不能指定插入照片的名字,而是系統(tǒng)給了我們一個當(dāng)前時間的毫秒數(shù)為名字,有一個問題郁悶了很久,我還是先把insertImage的源碼貼出來吧
復(fù)制代碼 代碼如下:
/**
* 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,我剛以為是可以設(shè)置圖片的名字,設(shè)置一下,原來不是,郁悶,哪位高手知道title這個字段是干嘛的,告訴下小弟,不勝感激!
當(dāng)然Android還提供了一個插入系統(tǒng)相冊的方法,可以指定保存圖片的名字,我把源碼貼出來吧
復(fù)制代碼 代碼如下:
/**
* 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) {
}
}
}
啊啊,貼完源碼我才發(fā)現(xiàn),這個方法調(diào)用了第一個方法,這個name就是上面方法的title,暈死,這下更加郁悶了,反正我設(shè)置title無效果,求高手為小弟解答,先不管了,我們繼續(xù)往下說
上面那段代碼插入到系統(tǒng)相冊之后還需要發(fā)條廣播
復(fù)制代碼 代碼如下:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
上面那條廣播是掃描整個sd卡的廣播,如果你sd卡里面東西很多會掃描很久,在掃描當(dāng)中我們是不能訪問sd卡,所以這樣子用戶體現(xiàn)很不好,用過微信的朋友都知道,微信保存圖片到系統(tǒng)相冊并沒有掃描整個SD卡,所以我們用到下面的方法
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
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,我們根據(jù)這個Uri就能獲取到圖片的絕對路徑
復(fù)制代碼 代碼如下:
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;
}
根據(jù)上面的那個方法獲取到的就是圖片的絕對路徑,這樣子我們就不用發(fā)送掃描整個SD卡的廣播了,呵呵,寫到這里就算是寫完了,寫的很亂,希望大家將就的看下,希望對你有幫助!
您可能感興趣的文章:
相關(guān)文章
Android XMPP通訊自定義Packet&Provider
這篇文章主要介紹了Android XMPP通訊自定義Packet&Provider的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android實現(xiàn)波浪線效果(xml bitmap)
這篇文章主要介紹了Android xml bitmap實現(xiàn)波浪線效果,制作過程簡單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android BottomNavigationView結(jié)合ViewPager實現(xiàn)底部導(dǎo)航欄步驟詳解
這篇文章主要介紹了Android BottomNavigationView結(jié)合ViewPager實現(xiàn)底部導(dǎo)航欄步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Android使用RecyclerView實現(xiàn)今日頭條頻道管理功能
這篇文章主要為大家詳細介紹了Android使用RecyclerView實現(xiàn)今日頭條頻道管理功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
android 檢查網(wǎng)絡(luò)連接狀態(tài)實現(xiàn)步驟
android 如何檢查網(wǎng)絡(luò)連接狀態(tài),是android開發(fā)中一個常見的問題,本文將介紹如何實現(xiàn),需要的朋友可以參考下2012-12-12
Android裁剪圖片為圓形圖片的實現(xiàn)原理與代碼
這個方法是根據(jù)傳入的圖片的高度(height)和寬度(width)決定的,如果是 width <= height時,則會裁剪高度,裁剪的區(qū)域是寬度不變高度從頂部到寬度width的長度2013-01-01

