根據Uri獲取檔案的絕對路徑

2021-09-07 11:00:31 字數 2328 閱讀 9691

public static 

string getrealpathfromuri(

context

context

, uri

contenturi) ,

//null

, null

, null

);if

(cursor

== null

) result

= contenturi

.getpath();

else

return

result

;}

/**

* 根據的uri獲取的絕對路徑(適配多種api)**

@return

如果uri對應的存在, 那麼返回該的絕對路徑, 否則返回null

*/public static

string getrealpathfromuri(

context

context

, uri

uri)

/*** 適配api19以上,根據uri獲取的絕對路徑

*/@targetapi

(build.version_codes

.kitkat

)private static

string getrealpathfromuri_aboveapi19(

context

context

, uri

uri)

;string

selection

= mediastore.images.media

._id

+ "=?"

;string

selectionargs = ;

cursor

cursor

= context

.getcontentresolver().query(

mediastore.images.media

.external_content_uri,//

projection

, selection

, selectionargs

, null

);int

columnindex

= cursor

.getcolumnindex(

projection[0

]);if

(cursor

.movetofirst())

filepath

= cursor

.getstring(

columnindex

);cursor

.close();

return

filepath;}

/*** 適配api11-api18,根據uri獲取的絕對路徑

*/private static

string getrealpathfromuri_api11to18(

context

context

, uri

uri)

;cursorloader

loader

= new

cursorloader

(context

, uri

, projection

, null

, null

, null

);cursor

cursor

= loader

.loadinbackground();

if (

cursor

!= null

) return

filepath;}

/*** 適配api11以下(不包括api11),根據uri獲取的絕對路徑

*/private static

string getrealpathfromuri_belowapi11(

context

context

, uri

uri)

;cursor

cursor

= context

.getcontentresolver().query(

uri,

projection

, null

, null

, null

);if

(cursor

!= null

) return

filepath

;}

null

根據Uri獲取檔案的絕對路徑

public static string getrealpathfromuri context context uri contenturi null null null if cursor null result contenturi getpath else return result 根據的u...

android 根據Uri獲取檔案絕對路徑

網上搜尋的 protected string getabsoluteimagepath uri uri cursor cursor managedquery uri,proj,which columns to return null,where clause which rows to return...

檔案絕對路徑獲取

yourclass.class.getclassloader getresource threadpoolconfig.xml getpath 通過上述方法獲取到的一般是指定檔案的絕對路徑 並且該檔案必須存放在src目錄下 存在的問題 如果絕對路徑中存在空格,getpath取到的值是經過urlenc...