麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

Android拍照保存在系統(tǒng)相冊不顯示的問題解決方法

2020-04-11 12:15:59
字體:
供稿:網(wǎng)友
可能大家都知道我們保存相冊到Android手機(jī)的時候,然后去打開系統(tǒng)圖庫找不到我們想要的那張圖片,那是因?yàn)槲覀儾迦氲膱D片還沒有更新的緣故,先講解下插入系統(tǒng)圖庫的方法吧,很簡單,一句代碼就能實(shí)現(xiàn)
復(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卡的廣播了,呵呵,寫到這里就算是寫完了,寫的很亂,希望大家將就的看下,希望對你有幫助!
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 99精彩视频在线观看 | 国产一级二级在线播放 | 亚洲网站在线观看视频 | 精品久久久久久久久久久aⅴ | 国产精品一区在线免费观看 | 久久久久亚洲视频 | 91性视频 | 色视频一区二区 | av性色全交蜜桃成熟时 | 精品一区二区久久久久久按摩 | 欧美黄色大片免费观看 | 久久国产精品网 | 中国av免费在线观看 | 成年人在线视频观看 | 精品国产一区三区| 深夜免费视频 | 国产91一区 | 亚洲成人福利电影 | 干色视频| 国产亚洲精品久久午夜玫瑰园 | 国产一区二区三区在线免费 | 嗯~啊~弄嗯~啊h高潮视频 | 欧美成人黄色小视频 | 99日韩精品视频 | 黄网站进入 | 精品国产99久久久久久宅男i | 精品国产乱码久久久久久丨区2区 | 欧美一级毛片大片免费播放 | 91 在线视频观看 | 新久草在线视频 | 99精品国产小情侣高潮露脸在线 | 欧美一级黄色影院 | 久久久久久久久浪潮精品 | 精品一区二区在线观看视频 | 欧美1—12sexvideos | 在线免费观看日韩视频 | 国产一区二区视频在线播放 | 久操福利视频 | 极品一级片 | 国产精品午夜在线观看 | 人人玩人人爽 |