|
@@ -26,240 +26,257 @@ import java.io.File;
|
|
|
*/
|
|
|
public class PathUtils {
|
|
|
|
|
|
- public final static String SDCARD_MNT = "/mnt/sdcard";
|
|
|
- public final static String SDCARD = Environment.getExternalStorageDirectory().getPath();
|
|
|
-
|
|
|
- /**
|
|
|
- * <b>BuildTime:</b> 2014-10-22<br>
|
|
|
- * <b>Description:</b> get SDCard path<br>
|
|
|
- *
|
|
|
- * @return String of path
|
|
|
- */
|
|
|
- public static String getSDCardPath() {
|
|
|
- return Environment.getExternalStorageDirectory().getPath();
|
|
|
+ public final static String SDCARD_MNT = "/mnt/sdcard";
|
|
|
+ public final static String SDCARD = Environment.getExternalStorageDirectory().getPath();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <b>BuildTime:</b> 2014-10-22<br>
|
|
|
+ * <b>Description:</b> get SDCard path<br>
|
|
|
+ *
|
|
|
+ * @return String of path
|
|
|
+ */
|
|
|
+ public static String getSDCardPath() {
|
|
|
+ return Environment.getExternalStorageDirectory().getPath();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <b>BuildTime:</b> 2014年10月23日<br>
|
|
|
+ * <b>Description:</b> <br>
|
|
|
+ *
|
|
|
+ * @param mUri
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getAbsolutePathFromNoStandardUri(Uri mUri) {
|
|
|
+ String filePath = null;
|
|
|
+
|
|
|
+ String mUriString = mUri.toString();
|
|
|
+ mUriString = Uri.decode(mUriString);
|
|
|
+
|
|
|
+ String pre1 = "file://" + SDCARD + File.separator;
|
|
|
+ String pre2 = "file://" + SDCARD_MNT + File.separator;
|
|
|
+
|
|
|
+ if (mUriString.startsWith(pre1)) {
|
|
|
+ filePath = Environment.getExternalStorageDirectory().getPath()
|
|
|
+ + File.separator + mUriString.substring(pre1.length());
|
|
|
+ } else if (mUriString.startsWith(pre2)) {
|
|
|
+ filePath = Environment.getExternalStorageDirectory().getPath()
|
|
|
+ + File.separator + mUriString.substring(pre2.length());
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * <b>BuildTime:</b> 2014年10月23日<br>
|
|
|
- * <b>Description:</b> <br>
|
|
|
- *
|
|
|
- * @param mUri
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getAbsolutePathFromNoStandardUri(Uri mUri) {
|
|
|
- String filePath = null;
|
|
|
-
|
|
|
- String mUriString = mUri.toString();
|
|
|
- mUriString = Uri.decode(mUriString);
|
|
|
-
|
|
|
- String pre1 = "file://" + SDCARD + File.separator;
|
|
|
- String pre2 = "file://" + SDCARD_MNT + File.separator;
|
|
|
-
|
|
|
- if (mUriString.startsWith(pre1)) {
|
|
|
- filePath = Environment.getExternalStorageDirectory().getPath()
|
|
|
- + File.separator + mUriString.substring(pre1.length());
|
|
|
- } else if (mUriString.startsWith(pre2)) {
|
|
|
- filePath = Environment.getExternalStorageDirectory().getPath()
|
|
|
- + File.separator + mUriString.substring(pre2.length());
|
|
|
- }
|
|
|
- return filePath;
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <b>BuildTime:</b> 2014年10月23日<br>
|
|
|
+ * <b>Description:</b> Use the uri to get the file path<br>
|
|
|
+ *
|
|
|
+ * @param c
|
|
|
+ * @param uri
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getAbsoluteUriPath(Context c, Uri uri) {
|
|
|
+ String imgPath = "";
|
|
|
+ String[] proj = {MediaStore.Images.Media.DATA};
|
|
|
+ Cursor cursor = new CursorLoader(c, uri, proj, null, null, null).loadInBackground();
|
|
|
+
|
|
|
+ if (cursor != null) {
|
|
|
+ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
|
+ if (cursor.getCount() > 0 && cursor.moveToFirst()) {
|
|
|
+ imgPath = cursor.getString(column_index);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * <b>BuildTime:</b> 2014年10月23日<br>
|
|
|
- * <b>Description:</b> Use the uri to get the file path<br>
|
|
|
- *
|
|
|
- * @param c
|
|
|
- * @param uri
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getAbsoluteUriPath(Context c, Uri uri) {
|
|
|
- String imgPath = "";
|
|
|
- String[] proj = {MediaStore.Images.Media.DATA};
|
|
|
- Cursor cursor = new CursorLoader(c, uri, proj, null, null, null).loadInBackground();
|
|
|
-
|
|
|
- if (cursor != null) {
|
|
|
- int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
|
- if (cursor.getCount() > 0 && cursor.moveToFirst()) {
|
|
|
- imgPath = cursor.getString(column_index);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return imgPath;
|
|
|
+ return imgPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <b>BuildTime:</b> 2014-8-30<br>
|
|
|
+ * <b>Description:</b> Get the external cache directory,it will be bulid a
|
|
|
+ * directory what is name "Android/data/PACKAGE_NAME/cache" for 2.2 system"<br>
|
|
|
+ *
|
|
|
+ * @param context
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static File getExternalCacheDir(Context context) {
|
|
|
+ if (hasExternalCacheDir()) {
|
|
|
+ return context.getExternalCacheDir();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * <b>BuildTime:</b> 2014-8-30<br>
|
|
|
- * <b>Description:</b> Get the external cache directory,it will be bulid a
|
|
|
- * directory what is name "Android/data/PACKAGE_NAME/cache" for 2.2 system"<br>
|
|
|
- *
|
|
|
- * @param context
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static File getExternalCacheDir(Context context) {
|
|
|
- if (hasExternalCacheDir()) {
|
|
|
- return context.getExternalCacheDir();
|
|
|
- }
|
|
|
-
|
|
|
- final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
|
|
|
- return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
|
|
|
+ final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
|
|
|
+ return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <b>BuildTime:</b> 2014-8-30<br>
|
|
|
+ * <b>Description:</b> Check directory,if null,create it<br>
|
|
|
+ *
|
|
|
+ * @param parent
|
|
|
+ * @param dirName
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static File findOrCreateDir(File parent, String dirName) {
|
|
|
+ File directory = new File(parent, dirName);
|
|
|
+ if (!directory.exists()) {
|
|
|
+ directory.mkdirs();
|
|
|
}
|
|
|
+ return directory;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * <b>BuildTime:</b> 2014-8-30<br>
|
|
|
- * <b>Description:</b> Check directory,if null,create it<br>
|
|
|
- *
|
|
|
- * @param parent
|
|
|
- * @param dirName
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static File findOrCreateDir(File parent, String dirName) {
|
|
|
- File directory = new File(parent, dirName);
|
|
|
- if (!directory.exists()) {
|
|
|
- directory.mkdirs();
|
|
|
- }
|
|
|
- return directory;
|
|
|
- }
|
|
|
+ private static boolean hasExternalCacheDir() {
|
|
|
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
|
|
|
+ }
|
|
|
|
|
|
- private static boolean hasExternalCacheDir() {
|
|
|
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
|
|
|
- }
|
|
|
+ @TargetApi(Build.VERSION_CODES.KITKAT)
|
|
|
+ public static String getPath(final Context context, final Uri uri) {
|
|
|
|
|
|
- @TargetApi(Build.VERSION_CODES.KITKAT)
|
|
|
- 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];
|
|
|
- }
|
|
|
-
|
|
|
- // TODO handle non-primary volumes
|
|
|
- }
|
|
|
- // 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;
|
|
|
- switch (type) {
|
|
|
- case "image":
|
|
|
- contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
|
- break;
|
|
|
- case "video":
|
|
|
- contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
|
|
- break;
|
|
|
- case "audio":
|
|
|
- contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- 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())) {
|
|
|
+ final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
|
|
|
|
|
- // Return the remote address
|
|
|
- if (isGooglePhotosUri(uri))
|
|
|
- return uri.getLastPathSegment();
|
|
|
+ // 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];
|
|
|
|
|
|
- return getDataColumn(context, uri, null, null);
|
|
|
- }
|
|
|
- // File
|
|
|
- else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
|
|
- return uri.getPath();
|
|
|
+ if ("primary".equalsIgnoreCase(type)) {
|
|
|
+ return Environment.getExternalStorageDirectory() + "/" + split[1];
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
- }
|
|
|
+ // TODO handle non-primary volumes
|
|
|
+ }
|
|
|
+ // 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;
|
|
|
+ switch (type) {
|
|
|
+ case "image":
|
|
|
+ contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
|
|
+ break;
|
|
|
+ case "video":
|
|
|
+ contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
|
|
+ break;
|
|
|
+ case "audio":
|
|
|
+ contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 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
|
|
|
+ final String selection = "_id=?";
|
|
|
+ final String[] selectionArgs = new String[]{
|
|
|
+ split[1]
|
|
|
};
|
|
|
|
|
|
- try {
|
|
|
- cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
|
|
|
- null);
|
|
|
- if (cursor != null && cursor.moveToFirst()) {
|
|
|
- final int index = cursor.getColumnIndexOrThrow(column);
|
|
|
- return cursor.getString(index);
|
|
|
- }
|
|
|
- } finally {
|
|
|
- if (cursor != null)
|
|
|
- cursor.close();
|
|
|
- }
|
|
|
- return null;
|
|
|
+ return getDataColumn(context, contentUri, selection, selectionArgs);
|
|
|
+ }
|
|
|
}
|
|
|
+ // MediaStore (and general)
|
|
|
+ else if ("content".equalsIgnoreCase(uri.getScheme())) {
|
|
|
|
|
|
+ // Return the remote address
|
|
|
+ if (isGooglePhotosUri(uri))
|
|
|
+ return uri.getLastPathSegment();
|
|
|
|
|
|
- /**
|
|
|
- * @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());
|
|
|
+ return getDataColumn(context, uri, null, null);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * @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());
|
|
|
+ // File
|
|
|
+ else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
|
|
+ return uri.getPath();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param uri The Uri to check.
|
|
|
- * @return Whether the Uri authority is Google Photos.
|
|
|
- */
|
|
|
- public static boolean isGooglePhotosUri(Uri uri) {
|
|
|
- return "com.google.android.apps.photos.content".equals(uri.getAuthority());
|
|
|
+ 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 index = cursor.getColumnIndexOrThrow(column);
|
|
|
+ return cursor.getString(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());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param uri
|
|
|
+ * The Uri to check.
|
|
|
+ *
|
|
|
+ * @return Whether the Uri authority is Google Photos.
|
|
|
+ */
|
|
|
+ public static boolean isGooglePhotosUri(Uri uri) {
|
|
|
+ return "com.google.android.apps.photos.content".equals(uri.getAuthority());
|
|
|
+ }
|
|
|
}
|