最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼

 更新時(shí)間:2017年07月12日 08:46:52   作者:Duang木魚  
在項(xiàng)目中需要用到將Uri轉(zhuǎn)換為絕對(duì)路徑,下面小編把Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼分享到腳本之家平臺(tái),需要的的朋友參考下吧

在項(xiàng)目中需要用到將Uri轉(zhuǎn)換為絕對(duì)路徑,在網(wǎng)上找到一個(gè)方法,做個(gè)筆記

網(wǎng)上有不少方法,但是有的對(duì)4.4后的版本無(wú)效,這里的方法可以在4.4之后的版本將Uri轉(zhuǎn)換為絕對(duì)路徑

public class GetPathFromUri { 
  /** 
   * 專為Android4.4設(shè)計(jì)的從Uri獲取文件絕對(duì)路徑 
   */ 
  public static String getPath(final Context context, final Uri uri) { 
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 
    // DocumentProvider 
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
      // ExternalStorageProvider 
      if (isExternalStorageDocument(uri)) { 
        final String docId = DocumentsContract.getDocumentId(uri); 
        final String[] split = docId.split(":"); 
        final String type = split[0]; 
        if ("primary".equalsIgnoreCase(type)) { 
          return Environment.getExternalStorageDirectory() + "/" + split[1]; 
        } 
      } 
      // DownloadsProvider 
      else if (isDownloadsDocument(uri)) { 
        final String id = DocumentsContract.getDocumentId(uri); 
        final Uri contentUri = ContentUris.withAppendedId( 
            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 
        return getDataColumn(context, contentUri, null, null); 
      } 
      // MediaProvider 
      else if (isMediaDocument(uri)) { 
        final String docId = DocumentsContract.getDocumentId(uri); 
        final String[] split = docId.split(":"); 
        final String 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; 
        } 
        final String selection = "_id=?"; 
        final String[] selectionArgs = new String[]{split[1]}; 
        return getDataColumn(context, contentUri, selection, selectionArgs); 
      } 
    } 
    // MediaStore (and general) 
    else if ("content".equalsIgnoreCase(uri.getScheme())) { 
      return getDataColumn(context, uri, null, null); 
    } 
    // File 
    else if ("file".equalsIgnoreCase(uri.getScheme())) { 
      return uri.getPath(); 
    } 
    return null; 
  } 
  /** 
   * Get the value of the data column for this Uri. This is useful for 
   * MediaStore Uris, and other file-based ContentProviders. 
   * 
   * @param context    The context. 
   * @param uri      The Uri to query. 
   * @param selection   (Optional) Filter used in the query. 
   * @param selectionArgs (Optional) Selection arguments used in the query. 
   * @return The value of the _data column, which is typically a file path. 
   */ 
  public static String getDataColumn(Context context, Uri uri, String selection, 
                String[] selectionArgs) { 
    Cursor cursor = null; 
    final String column = "_data"; 
    final String[] projection = {column}; 
    try { 
      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 
          null); 
      if (cursor != null && cursor.moveToFirst()) { 
        final int column_index = cursor.getColumnIndexOrThrow(column); 
        return cursor.getString(column_index); 
      } 
    } finally { 
      if (cursor != null) 
        cursor.close(); 
    } 
    return null; 
  } 
  /** 
   * @param uri The Uri to check. 
   * @return Whether the Uri authority is ExternalStorageProvider. 
   */ 
  public static boolean isExternalStorageDocument(Uri uri) { 
    return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
  } 
  /** 
   * @param uri The Uri to check. 
   * @return Whether the Uri authority is DownloadsProvider. 
   */ 
  public static boolean isDownloadsDocument(Uri uri) { 
    return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
  } 
  /** 
   * @param uri The Uri to check. 
   * @return Whether the Uri authority is MediaProvider. 
   */ 
  public static boolean isMediaDocument(Uri uri) { 
    return "com.android.providers.media.documents".equals(uri.getAuthority()); 
  } 
} 

絕對(duì)路徑轉(zhuǎn)Uri比較簡(jiǎn)單

以絕對(duì)路徑創(chuàng)建一個(gè)File對(duì)象,然后調(diào)用

Uri.fromFile(file)

以上所述是小編給大家介紹的Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android使用AIDL方式實(shí)現(xiàn)播放音樂(lè)案例

    Android使用AIDL方式實(shí)現(xiàn)播放音樂(lè)案例

    這篇文章主要介紹了Android使用AIDL方式實(shí)現(xiàn)播放音樂(lè)案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Kotlin自定義實(shí)現(xiàn)支付密碼數(shù)字鍵盤的方法實(shí)例

    Kotlin自定義實(shí)現(xiàn)支付密碼數(shù)字鍵盤的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Kotlin如何自定義實(shí)現(xiàn)支付密碼數(shù)字鍵盤的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android開發(fā)中用Kotlin編寫LiveData組件教程

    Android開發(fā)中用Kotlin編寫LiveData組件教程

    LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)
    2022-12-12
  • Android zip文件下載和解壓實(shí)例

    Android zip文件下載和解壓實(shí)例

    這篇文章主要介紹了Android zip文件下載和解壓實(shí)例,有需要的朋友可以參考一下
    2014-01-01
  • android I/0流操作文件(文件存儲(chǔ))

    android I/0流操作文件(文件存儲(chǔ))

    Java提供一套完整的I/О流體系,通過(guò)I/О流可以非常方便地訪問(wèn)磁盤中的文件,同樣Android 也支持I/O流方式來(lái)訪問(wèn)手機(jī)等移動(dòng)設(shè)備中的存儲(chǔ)文件。希望可以為大家提供幫助
    2021-06-06
  • Android 自定義控件實(shí)現(xiàn)顯示文字的功能

    Android 自定義控件實(shí)現(xiàn)顯示文字的功能

    這篇文章主要介紹了Android 自定義控件實(shí)現(xiàn)顯示文字的功能的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Android自定義view實(shí)現(xiàn)水波進(jìn)度條控件

    Android自定義view實(shí)現(xiàn)水波進(jìn)度條控件

    這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)水波進(jìn)度條控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android通過(guò)單點(diǎn)觸摸移動(dòng)圖片

    Android通過(guò)單點(diǎn)觸摸移動(dòng)圖片

    這篇文章主要為大家詳細(xì)介紹了Android通過(guò)單點(diǎn)觸摸移動(dòng)圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤

    Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤

    這篇文章主要介紹了Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤,總結(jié)分析了Android開發(fā)中幫助文件、開發(fā)工具、社區(qū)等的重要性以及重要的開發(fā)原則,需要的朋友可以參考下
    2016-01-01
  • Android實(shí)現(xiàn)圖片加載進(jìn)度提示

    Android實(shí)現(xiàn)圖片加載進(jìn)度提示

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片加載進(jìn)度提示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評(píng)論

乌拉特前旗| 英吉沙县| 滁州市| 新乐市| 湖南省| 托克逊县| 金阳县| 涞水县| 伊吾县| 馆陶县| 常德市| 册亨县| 聂荣县| 舟山市| 商洛市| 蓝田县| 安化县| 柘城县| 区。| 黔西| 侯马市| 时尚| 卢龙县| 大名县| 泰安市| 茂名市| 抚顺县| 宣恩县| 仪陇县| 张北县| 白银市| 广南县| 南郑县| 康定县| 惠来县| 平度市| 桃园县| 济宁市| 恩施市| 长乐市| 登封市|