BVB Source Codes

Luban Show PathUtils.java Source code

Return Download Luban: download PathUtils.java Source code - Download Luban Source code - Type:.java
  1. package top.zibin.luban.example;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.ContentUris;
  5. import android.content.Context;
  6. import android.content.CursorLoader;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Environment;
  11. import android.provider.DocumentsContract;
  12. import android.provider.MediaStore;
  13.  
  14. import java.io.File;
  15.  
  16. /**
  17.  * <b>ClassName</b> PathUtils.java<br>
  18.  * <p/>
  19.  * <b>BuildTime:</b> 2014-9-7<br>
  20.  * <b>Author:</b> Curzbin<br>
  21.  * <p/>
  22.  * <b>UpdateTime:</b> <br>
  23.  * <b>UpdateUser:</b> <br>
  24.  * <p/>
  25.  * <b>Description:</b> The auxiliary class of Path<br>
  26.  */
  27. public class PathUtils {
  28.  
  29.     public final static String SDCARD_MNT = "/mnt/sdcard";
  30.     public final static String SDCARD = Environment.getExternalStorageDirectory().getPath();
  31.  
  32.     /**
  33.      * <b>BuildTime:</b> 2014-10-22<br>
  34.      * <b>Description:</b> get SDCard path<br>
  35.      *
  36.      * @return String of path
  37.      */
  38.     public static String getSDCardPath() {
  39.         return Environment.getExternalStorageDirectory().getPath();
  40.     }
  41.  
  42.     /**
  43.      * <b>BuildTime:</b> 2014骞?0鏈?3鏃?br>
  44.      * <b>Description:</b> <br>
  45.      *
  46.      * @param mUri
  47.      * @return
  48.      */
  49.     public static String getAbsolutePathFromNoStandardUri(Uri mUri) {
  50.         String filePath = null;
  51.  
  52.         String mUriString = mUri.toString();
  53.         mUriString = Uri.decode(mUriString);
  54.  
  55.         String pre1 = "file://" + SDCARD + File.separator;
  56.         String pre2 = "file://" + SDCARD_MNT + File.separator;
  57.  
  58.         if (mUriString.startsWith(pre1)) {
  59.             filePath = Environment.getExternalStorageDirectory().getPath()
  60.                     + File.separator + mUriString.substring(pre1.length());
  61.         } else if (mUriString.startsWith(pre2)) {
  62.             filePath = Environment.getExternalStorageDirectory().getPath()
  63.                     + File.separator + mUriString.substring(pre2.length());
  64.         }
  65.         return filePath;
  66.     }
  67.  
  68.     /**
  69.      * <b>BuildTime:</b> 2014骞?0鏈?3鏃?br>
  70.      * <b>Description:</b> Use the uri to get the file path<br>
  71.      *
  72.      * @param c
  73.      * @param uri
  74.      * @return
  75.      */
  76.     public static String getAbsoluteUriPath(Context c, Uri uri) {
  77.         String imgPath = "";
  78.         String[] proj = {MediaStore.Images.Media.DATA};
  79.         Cursor cursor = new CursorLoader(c, uri, proj, null, null, null).loadInBackground();
  80.  
  81.         if (cursor != null) {
  82.             int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  83.             if (cursor.getCount() > 0 && cursor.moveToFirst()) {
  84.                 imgPath = cursor.getString(column_index);
  85.             }
  86.         }
  87.  
  88.         return imgPath;
  89.     }
  90.  
  91.     /**
  92.      * <b>BuildTime:</b> 2014-8-30<br>
  93.      * <b>Description:</b> Get the external cache directory,it will be bulid a
  94.      * directory what is name "Android/data/PACKAGE_NAME/cache" for 2.2 system"<br>
  95.      *
  96.      * @param context
  97.      * @return
  98.      */
  99.     public static File getExternalCacheDir(Context context) {
  100.         if (hasExternalCacheDir()) {
  101.             return context.getExternalCacheDir();
  102.         }
  103.  
  104.         final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
  105.         return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
  106.     }
  107.  
  108.     /**
  109.      * <b>BuildTime:</b> 2014-8-30<br>
  110.      * <b>Description:</b> Check directory,if null,create it<br>
  111.      *
  112.      * @param parent
  113.      * @param dirName
  114.      * @return
  115.      */
  116.     public static File findOrCreateDir(File parent, String dirName) {
  117.         File directory = new File(parent, dirName);
  118.         if (!directory.exists()) {
  119.             directory.mkdirs();
  120.         }
  121.         return directory;
  122.     }
  123.  
  124.     private static boolean hasExternalCacheDir() {
  125.         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
  126.     }
  127.  
  128.     @TargetApi(Build.VERSION_CODES.KITKAT)
  129.     public static String getPath(final Context context, final Uri uri) {
  130.  
  131.         final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  132.  
  133.         // DocumentProvider
  134.         if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  135.             // ExternalStorageProvider
  136.             if (isExternalStorageDocument(uri)) {
  137.                 final String docId = DocumentsContract.getDocumentId(uri);
  138.                 final String[] split = docId.split(":");
  139.                 final String type = split[0];
  140.  
  141.                 if ("primary".equalsIgnoreCase(type)) {
  142.                     return Environment.getExternalStorageDirectory() + "/" + split[1];
  143.                 }
  144.  
  145.                 // TODO handle non-primary volumes
  146.             }
  147.             // DownloadsProvider
  148.             else if (isDownloadsDocument(uri)) {
  149.  
  150.                 final String id = DocumentsContract.getDocumentId(uri);
  151.                 final Uri contentUri = ContentUris.withAppendedId(
  152.                         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  153.  
  154.                 return getDataColumn(context, contentUri, null, null);
  155.             }
  156.             // MediaProvider
  157.             else if (isMediaDocument(uri)) {
  158.                 final String docId = DocumentsContract.getDocumentId(uri);
  159.                 final String[] split = docId.split(":");
  160.                 final String type = split[0];
  161.  
  162.                 Uri contentUri = null;
  163.                 switch (type) {
  164.                     case "image":
  165.                         contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  166.                         break;
  167.                     case "video":
  168.                         contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  169.                         break;
  170.                     case "audio":
  171.                         contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  172.                         break;
  173.                 }
  174.  
  175.                 final String selection = "_id=?";
  176.                 final String[] selectionArgs = new String[]{
  177.                         split[1]
  178.                 };
  179.  
  180.                 return getDataColumn(context, contentUri, selection, selectionArgs);
  181.             }
  182.         }
  183.         // MediaStore (and general)
  184.         else if ("content".equalsIgnoreCase(uri.getScheme())) {
  185.  
  186.             // Return the remote address
  187.             if (isGooglePhotosUri(uri))
  188.                 return uri.getLastPathSegment();
  189.  
  190.             return getDataColumn(context, uri, null, null);
  191.         }
  192.         // File
  193.         else if ("file".equalsIgnoreCase(uri.getScheme())) {
  194.             return uri.getPath();
  195.         }
  196.  
  197.         return null;
  198.     }
  199.  
  200.     /**
  201.      * Get the value of the data column for this Uri. This is useful for
  202.      * MediaStore Uris, and other file-based ContentProviders.
  203.      *
  204.      * @param context       The context.
  205.      * @param uri           The Uri to query.
  206.      * @param selection     (Optional) Filter used in the query.
  207.      * @param selectionArgs (Optional) Selection arguments used in the query.
  208.      * @return The value of the _data column, which is typically a file path.
  209.      */
  210.     public static String getDataColumn(Context context, Uri uri, String selection,
  211.                                        String[] selectionArgs) {
  212.  
  213.         Cursor cursor = null;
  214.         final String column = "_data";
  215.         final String[] projection = {
  216.                 column
  217.         };
  218.  
  219.         try {
  220.             cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  221.                     null);
  222.             if (cursor != null && cursor.moveToFirst()) {
  223.                 final int index = cursor.getColumnIndexOrThrow(column);
  224.                 return cursor.getString(index);
  225.             }
  226.         } finally {
  227.             if (cursor != null)
  228.                 cursor.close();
  229.         }
  230.         return null;
  231.     }
  232.  
  233.  
  234.     /**
  235.      * @param uri The Uri to check.
  236.      * @return Whether the Uri authority is ExternalStorageProvider.
  237.      */
  238.     public static boolean isExternalStorageDocument(Uri uri) {
  239.         return "com.android.externalstorage.documents".equals(uri.getAuthority());
  240.     }
  241.  
  242.     /**
  243.      * @param uri The Uri to check.
  244.      * @return Whether the Uri authority is DownloadsProvider.
  245.      */
  246.     public static boolean isDownloadsDocument(Uri uri) {
  247.         return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  248.     }
  249.  
  250.     /**
  251.      * @param uri The Uri to check.
  252.      * @return Whether the Uri authority is MediaProvider.
  253.      */
  254.     public static boolean isMediaDocument(Uri uri) {
  255.         return "com.android.providers.media.documents".equals(uri.getAuthority());
  256.     }
  257.  
  258.     /**
  259.      * @param uri The Uri to check.
  260.      * @return Whether the Uri authority is Google Photos.
  261.      */
  262.     public static boolean isGooglePhotosUri(Uri uri) {
  263.         return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  264.     }
  265. }
  266.  
downloadPathUtils.java Source code - Download Luban Source code
Related Source Codes/Software:
vue-router - The official router for Vue.js. ... 2017-01-11
socketcluster - Highly scalable realtime framework ... 2017-01-11
forum - Blue light (the Lantern) the official BBS 2017-01-11
tensorflow-zh - Google new open source ai system TensorFlow Chines... 2017-01-11
react-native-elements - React Native Elements UI Toolkit 2017-01-11
show-me-the-code - Python's book and a small program every day 2017-01-11
places - 2017-01-11
buck - A fast build system that encourages the creation o... 2017-01-11
react-native-web - React Native for We 2017-01-11
awesome-python-cn - Python resources of Chinese version, include: Web ... 2017-01-11
KeychainAccess - Simple Swift wrapper for Keychain that works on iO... 2017-02-20
emojicon - A library to show emoji in TextView, EditText (lik... 2017-02-20
postgres - Mirror of the official PostgreSQL GIT repository. ... 2017-02-20
shuttle - A simple SSH shortcut menu for macOS ... 2017-02-20
sticky-headers-recyclerview - [UNMAINTAINED] Sticky Headers decorator for Androi... 2017-02-20
octokit.rb - Ruby toolkit for the GitHub API ... 2017-02-20
ricochet - Anonymous peer-to-peer instant messaging ... 2017-02-20
mobile-chrome-apps - Chrome apps on Android and iOS 2017-02-20
pycrumbs - Bits and bytes of Python from the Internet 2017-02-20
drupal - Verbatim mirror of the git.drupal.org repository f... 2017-02-20

 Back to top