欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2013年06月03日 17:51:57   作者:  
我們保存相冊(cè)到Android手機(jī)的時(shí)候,然后去打開(kāi)系統(tǒng)圖庫(kù)找不到我們想要的那張圖片,那是因?yàn)槲覀儾迦氲膱D片還沒(méi)有更新的緣故,下面與大家分享下此問(wèn)題的解決方法
可能大家都知道我們保存相冊(cè)到Android手機(jī)的時(shí)候,然后去打開(kāi)系統(tǒng)圖庫(kù)找不到我們想要的那張圖片,那是因?yàn)槲覀儾迦氲膱D片還沒(méi)有更新的緣故,先講解下插入系統(tǒng)圖庫(kù)的方法吧,很簡(jiǎn)單,一句代碼就能實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");

通過(guò)上面的那句代碼就能插入到系統(tǒng)圖庫(kù),這時(shí)候有一個(gè)問(wèn)題,就是我們不能指定插入照片的名字,而是系統(tǒng)給了我們一個(gè)當(dāng)前時(shí)間的毫秒數(shù)為名字,有一個(gè)問(wèn)題郁悶了很久,我還是先把insertImage的源碼貼出來(lái)吧
復(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;
}

上面方法里面有一個(gè)title,我剛以為是可以設(shè)置圖片的名字,設(shè)置一下,原來(lái)不是,郁悶,哪位高手知道title這個(gè)字段是干嘛的,告訴下小弟,不勝感激!
當(dāng)然Android還提供了一個(gè)插入系統(tǒng)相冊(cè)的方法,可以指定保存圖片的名字,我把源碼貼出來(lái)吧
復(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),這個(gè)方法調(diào)用了第一個(gè)方法,這個(gè)name就是上面方法的title,暈死,這下更加郁悶了,反正我設(shè)置title無(wú)效果,求高手為小弟解答,先不管了,我們繼續(xù)往下說(shuō)
上面那段代碼插入到系統(tǒng)相冊(cè)之后還需要發(fā)條廣播
復(fù)制代碼 代碼如下:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

上面那條廣播是掃描整個(gè)sd卡的廣播,如果你sd卡里面東西很多會(huì)掃描很久,在掃描當(dāng)中我們是不能訪問(wèn)sd卡,所以這樣子用戶體現(xiàn)很不好,用過(guò)微信的朋友都知道,微信保存圖片到系統(tǒng)相冊(cè)并沒(méi)有掃描整個(gè)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();
}
});

也行你會(huì)問(wèn)我,怎么獲取到我們剛剛插入的圖片的路徑?呵呵,這個(gè)自有方法獲取,insertImage(ContentResolver cr, Bitmap source,String title, String description),這個(gè)方法給我們返回的就是插入圖片的Uri,我們根據(jù)這個(gè)Uri就能獲取到圖片的絕對(duì)路徑
復(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ù)上面的那個(gè)方法獲取到的就是圖片的絕對(duì)路徑,這樣子我們就不用發(fā)送掃描整個(gè)SD卡的廣播了,呵呵,寫到這里就算是寫完了,寫的很亂,希望大家將就的看下,希望對(duì)你有幫助!

相關(guān)文章

  • android開(kāi)發(fā)教程之實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例

    android開(kāi)發(fā)教程之實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例

    這篇文章主要介紹了android實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例,需要的朋友可以參考下
    2014-03-03
  • Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼

    Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android開(kāi)發(fā)實(shí)現(xiàn)錄屏小功能

    Android開(kāi)發(fā)實(shí)現(xiàn)錄屏小功能

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)錄屏小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • android4.0屏蔽HOME鍵的簡(jiǎn)單實(shí)現(xiàn)

    android4.0屏蔽HOME鍵的簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要介紹了android4.0屏蔽HOME鍵的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • 可伸縮的textview詳解(推薦)

    可伸縮的textview詳解(推薦)

    下面小編就為大家?guī)?lái)一篇可伸縮的textview詳解(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • Android SharedPreferences數(shù)據(jù)存儲(chǔ)詳解

    Android SharedPreferences數(shù)據(jù)存儲(chǔ)詳解

    SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出
    2022-11-11
  • AndroidStudio修改Code Style來(lái)格式化自定義標(biāo)簽的xml文件方式

    AndroidStudio修改Code Style來(lái)格式化自定義標(biāo)簽的xml文件方式

    這篇文章主要介紹了AndroidStudio修改Code Style來(lái)格式化自定義標(biāo)簽的xml文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android入門之彈出式對(duì)話框的實(shí)現(xiàn)

    Android入門之彈出式對(duì)話框的實(shí)現(xiàn)

    Android Studio里有一種Dialog叫PopWindow,它是一種“可阻塞式Dialog”,即彈出后除非你給它一個(gè)“動(dòng)作”否則就一直顯示在那。本文就將實(shí)現(xiàn)這樣的彈出式對(duì)話框,感興趣的可以了解一下
    2022-11-11
  • Android仿京東搜索框漸變效果

    Android仿京東搜索框漸變效果

    這篇文章主要為大家詳細(xì)介紹了Android仿京東搜索框漸變效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Flutter混合開(kāi)發(fā)詳解

    Flutter混合開(kāi)發(fā)詳解

    這篇文章主要介紹了Flutter混合開(kāi)發(fā)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01

最新評(píng)論