欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Uri和文件路径互相转换的实例代码

程序员文章站 2022-04-13 22:45:23
在项目中需要用到将uri转换为绝对路径,在网上找到一个方法,做个笔记 网上有不少方法,但是有的对4.4后的版本无效,这里的方法可以在4.4之后的版本将uri转换为绝对路径...

在项目中需要用到将uri转换为绝对路径,在网上找到一个方法,做个笔记

网上有不少方法,但是有的对4.4后的版本无效,这里的方法可以在4.4之后的版本将uri转换为绝对路径

public class getpathfromuri { 
  /** 
   * 专为android4.4设计的从uri获取文件绝对路径 
   */ 
  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()); 
  } 
} 

绝对路径转uri比较简单

以绝对路径创建一个file对象,然后调用

uri.fromfile(file)

以上所述是小编给大家介绍的android uri和文件路径互相转换的实例代码,希望对大家有所帮助