1. 程式人生 > >Android中res/drawable(mipmap)/xxxx.png的Uri和url的轉換

Android中res/drawable(mipmap)/xxxx.png的Uri和url的轉換

宣告:下列方法已經經過驗證,可用

Uri------>url.
    /**
     * res/drawable(mipmap)/xxx.png::::uri---->url
     *
     * @return
     */
    private String imageTranslateUri(int resId) {

        Resources r = getResources();
        Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                +
r.getResourcePackageName(resId) + "/" + r.getResourceTypeName(resId) + "/" + r.getResourceEntryName(resId)); return uri.toString(); }

url—>uri.
String url="xxxxxxxxxx";
Uri parseUri = Uri.parse(url);

————————————————————————————————————

擴充套件:File, Uri和Path的相互轉化

file轉uri:
Uri uri = file.toURI();  

uri轉path:(適用於api<=19,對於api>19可參照推薦)
private String getPath(Uri uri) {  
       String[] projection = {MediaStore.Video.Media.DATA};  
       Cursor cursor = managedQuery(uri, projection, null, null, null);  
       int column_index =
cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }

推薦:Android根據圖片Uri獲取圖片path絕對路徑

path轉uri:

Uri uri = Uri.parse(path);  

file轉path:
String path = file.getPath()  

path轉file:
File file = new File(path)