Android解決4.4以上版本得不到路徑的情況

public classUriUtils {

/**

* Get a file path from a Uri. This will get the the path for Storage Access

* Framework Documents, as well as the _data field for the MediaStore and

* other file-based ContentProviders.

*

*@paramcontextThe context.

*@paramuriThe Uri to query.

*/

public staticStringgetPath(finalContext context, finalUri uri) {

final booleanisKitKat = Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT;

// DocumentProvider

if(isKitKat && DocumentsContract.isDocumentUri(context,uri)) {

// ExternalStorageProvider

if(isExternalStorageDocument(uri)) {

finalString docId = DocumentsContract.getDocumentId(uri);

finalString[] split = docId.split(":");

finalString type = split[0];

if("primary".equalsIgnoreCase(type)) {

returnEnvironment.getExternalStorageDirectory() +"/"+ split[1];

}

//TODO handle non-primary volumes

}

// DownloadsProvider

else if(isDownloadsDocument(uri)) {

finalString id = DocumentsContract.getDocumentId(uri);

finalUri contentUri = ContentUris.withAppendedId(

Uri.parse("content://downloads/public_downloads"),Long.valueOf(id));

returngetDataColumn(context,contentUri, null, null);

}

// MediaProvider

else if(isMediaDocument(uri)) {

finalString docId = DocumentsContract.getDocumentId(uri);

finalString[] split = docId.split(":");

finalString type = split[0];

Uri contentUri =null;

if("image".equals(type)) {

contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

}else if("video".equals(type)) {

contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

}else if("audio".equals(type)) {

contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

}

finalString selection ="_id=?";

finalString[] selectionArgs =newString[] {

split[1]

};

returngetDataColumn(context,contentUri,selection,selectionArgs);

}

}

// MediaStore (and general)

else if("content".equalsIgnoreCase(uri.getScheme())) {

returngetDataColumn(context,uri, null, null);

}

// File

else if("file".equalsIgnoreCase(uri.getScheme())) {

returnuri.getPath();

}

return null;

}

/**

* Get the value of the data column for this Uri. This is useful for

* MediaStore Uris, and other file-based ContentProviders.

*

*@paramcontextThe context.

*@paramuriThe Uri to query.

*@paramselection(Optional) Filter used in the query.

*@paramselectionArgs(Optional) Selection arguments used in the query.

*@returnThe value of the _data column, which is typically a file path.

*/

public staticStringgetDataColumn(Context context,Uri uri,String selection,

String[] selectionArgs) {

Cursor cursor =null;

finalString column ="_data";

finalString[] projection = {

column

};

try{

cursor = context.getContentResolver().query(uri,projection,selection,selectionArgs,

null);

if(cursor !=null&& cursor.moveToFirst()) {

final intcolumn_index = cursor.getColumnIndexOrThrow(column);

returncursor.getString(column_index);

}

}finally{

if(cursor !=null)

cursor.close();

}

return null;

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is ExternalStorageProvider.

*/

public static booleanisExternalStorageDocument(Uri uri) {

return"com.android.externalstorage.documents".equals(uri.getAuthority());

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is DownloadsProvider.

*/

public static booleanisDownloadsDocument(Uri uri) {

return"com.android.providers.downloads.documents".equals(uri.getAuthority());

}

/**

*@paramuriThe Uri to check.

*@returnWhether the Uri authority is MediaProvider.

*/

public static booleanisMediaDocument(Uri uri) {

return"com.android.providers.media.documents".equals(uri.getAuthority());

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市贪绘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌十偶,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件月杉,死亡現(xiàn)場離奇詭異突倍,居然都是意外死亡,警方通過查閱死者的電腦和手機隧哮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來座舍,“玉大人沮翔,你說我怎么就攤上這事∏” “怎么了采蚀?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長承二。 經(jīng)常有香客問我榆鼠,道長,這世上最難降的妖魔是什么矢洲? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任璧眠,我火速辦了婚禮,結(jié)果婚禮上读虏,老公的妹妹穿的比我還像新娘。我一直安慰自己袁滥,他們只是感情好盖桥,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著题翻,像睡著了一般揩徊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嵌赠,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天塑荒,我揣著相機與錄音,去河邊找鬼姜挺。 笑死齿税,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的炊豪。 我是一名探鬼主播凌箕,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼拧篮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了牵舱?” 一聲冷哼從身側(cè)響起串绩,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎芜壁,沒想到半個月后礁凡,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡慧妄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年把篓,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腰涧。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡韧掩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出窖铡,到底是詐尸還是另有隱情疗锐,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布费彼,位于F島的核電站滑臊,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏箍铲。R本人自食惡果不足惜雇卷,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望颠猴。 院中可真熱鬧关划,春花似錦、人聲如沸翘瓮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽资盅。三九已至调榄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呵扛,已是汗流浹背每庆。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留今穿,地道東北人缤灵。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凤价。 傳聞我的和親對象是個殘疾皇子鸽斟,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 原文章地址:Android 4.4從圖庫選擇圖片,獲取圖片路徑并裁剪 最近在做一個從圖庫選擇圖片或拍照,然后裁剪的...
    如若成楓閱讀 1,226評論 0 3
  • Android 一鍵分享時分享的圖片 // imagePath是圖片的本地路徑,Linked-In以外的平臺都支持...
    自然之秋閱讀 378評論 0 0
  • 只簡述我發(fā)現(xiàn)問題的根源利诺,有些是適配了7.0富蓄,會報權(quán)限失敗問題,那是由于沒有動態(tài)授權(quán)導致慢逾,接下來我一步一步給大家實現(xiàn)...
    Wocus閱讀 2,367評論 4 5
  • 地圖是一張白紙立倍,這當然很傷腦筋。任何人都會不知所措侣滩。 可是換個角度來看口注,正因為是一張白紙,才可以隨心所欲地描繪地圖...
    吹泡泡的貓閱讀 212評論 0 0
  • w_01閱讀 253評論 0 0