Class UriToFileUtil



  • public class UriToFileUtil
    extends java.lang.Object
    Util for converting Uri to usable File if possible

    Taken from here:

    See Also:
    UriToFileUtil Licensed under the Apache License, Version 2.0 @see ASL 2.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static java.lang.String getDataColumn(Context context, Uri uri, java.lang.String selection, java.lang.String[] selectionArgs)
      Get the value of the data column for this Uri.
      static java.lang.String getExtension(java.lang.String uri)
      Gets the extension of a file name, like "png" or "jpg".
      static java.io.File getFile(Context context, Uri uri)
      Convert Uri into File, if possible.
      static java.lang.String getMimeType(java.io.File file)
      The MIME type for the given file.
      static java.lang.String getPath(Context context, Uri uri)
      Get a file path from a Uri.
      static boolean isDownloadsDocument(Uri uri) 
      static boolean isExternalStorageDocument(Uri uri) 
      static boolean isGooglePhotosUri(Uri uri) 
      static boolean isLocal(java.lang.String url)
      Determines if the url is a remote url
      static boolean isMediaDocument(Uri uri) 
      static boolean isMediaUri(Uri uri)
      If uri authority is media
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getExtension

        public static java.lang.String getExtension(java.lang.String uri)
        Gets the extension of a file name, like "png" or "jpg".
        Parameters:
        uri - The URI of the file to get the extension for
        Returns:
        Extension excluding the dot("."); "" if there is no extension; null if uri was null.
        See Also:
        MimeTypeMap
      • isLocal

        public static boolean isLocal(java.lang.String url)
        Determines if the url is a remote url
        Parameters:
        url - to classify
        Returns:
        Whether the URI is a local one.
      • isMediaUri

        public static boolean isMediaUri(Uri uri)
        If uri authority is media
        Parameters:
        uri - to classify
        Returns:
        True if Uri is a MediaStore Uri.
      • getMimeType

        public static java.lang.String getMimeType(java.io.File file)
        The MIME type for the given file.
        Parameters:
        file - to determine the mime type
        Returns:
        The type of the given file, if unknown will return "application/octet-stream"
      • isExternalStorageDocument

        public static boolean isExternalStorageDocument(Uri uri)
        Parameters:
        uri - The Uri to check.
        Returns:
        Whether the Uri authority is ExternalStorageProvider.
      • isDownloadsDocument

        public static boolean isDownloadsDocument(Uri uri)
        Parameters:
        uri - The Uri to check.
        Returns:
        Whether the Uri authority is DownloadsProvider.
      • isMediaDocument

        public static boolean isMediaDocument(Uri uri)
        Parameters:
        uri - The Uri to check.
        Returns:
        Whether the Uri authority is MediaProvider.
      • isGooglePhotosUri

        public static boolean isGooglePhotosUri(Uri uri)
        Parameters:
        uri - The Uri to check.
        Returns:
        Whether the Uri authority is Google Photos.
      • getDataColumn

        public static java.lang.String getDataColumn(Context context,
                                                     Uri uri,
                                                     java.lang.String selection,
                                                     java.lang.String[] selectionArgs)
        Get the value of the data column for this Uri. This is useful for MediaStore Uris, and other file-based ContentProviders.
        Parameters:
        context - The context.
        uri - The Uri to query.
        selection - (Optional) Filter used in the query.
        selectionArgs - (Optional) Selection arguments used in the query.
        Returns:
        The value of the _data column, which is typically a file path.
      • getPath

        public static java.lang.String getPath(Context context,
                                               Uri uri)
        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.

        Callers should check whether the path is local before assuming it represents a local file.
        Parameters:
        context - The context.
        uri - The Uri to query.
        Returns:
        file path
        See Also:
        UriToFileUtil.isLocal(String), UriToFileUtil.getFile(Context, Uri)
      • getFile

        public static java.io.File getFile(Context context,
                                           Uri uri)
        Convert Uri into File, if possible.
        Parameters:
        context - for file lookup
        uri - identifier
        Returns:
        file A local file that the Uri was pointing to, or null if the Uri is unsupported or pointed to a remote resource.
        See Also:
        UriToFileUtil.getPath(Context, Uri)