diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/RxFileUtils.java b/RxTools-library/src/main/java/com/vondear/rxtools/RxFileUtils.java index 66ad6235..d0603106 100644 --- a/RxTools-library/src/main/java/com/vondear/rxtools/RxFileUtils.java +++ b/RxTools-library/src/main/java/com/vondear/rxtools/RxFileUtils.java @@ -18,6 +18,7 @@ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; +import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.Intent; @@ -26,6 +27,7 @@ import android.os.Build; import android.os.Environment; import android.os.StatFs; +import android.provider.DocumentsContract; import android.provider.MediaStore; import android.util.Log; @@ -65,6 +67,7 @@ */ public class RxFileUtils { + public static final int BUFSIZE = 1024 * 8; private static final String TAG = "RxFileUtils"; /** @@ -255,7 +258,7 @@ public static boolean cleanInternalDbs(Context context) { * 根据名称清除数据库 *

/data/data/com.xxx.xxx/databases/dbName

* - * @param dbName 数据库名称 + * @param dbName 数据库名称 * @return {@code true}: 清除成功
{@code false}: 清除失败 */ public static boolean cleanInternalDbByName(Context context, String dbName) { @@ -578,7 +581,6 @@ public static String getDiskCacheDir(Context context) { return cachePath; } - /** * 获取缓存视频文件目录 * @@ -595,8 +597,6 @@ public static String getDiskFileDir(Context context) { return cachePath; } - public static final int BUFSIZE = 1024 * 8; - /** * 多个文件合并 * @@ -698,37 +698,6 @@ public static void write(String filePath, String content) { } } - /** - * 传入文件名以及字符串, 将字符串信息保存到文件中 - * - * @param strFilePath - * @param strBuffer - */ - public void TextToFile(final String strFilePath, final String strBuffer) { - FileWriter fileWriter = null; - try { - // 创建文件对象 - File fileText = new File(strFilePath); - // 向文件写入对象写入信息 - fileWriter = new FileWriter(fileText); - // 写文件 - fileWriter.write(strBuffer); - // 关闭 - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - fileWriter.flush(); - fileWriter.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } - } - - - //---------------------------------------------------------------------------------------------- - /** * 获取 搜索的路径 下的 所有 后缀 的文件 * @@ -752,41 +721,9 @@ public static Vector GetAllFileName(String fileAbsolutePath, String suff } return vecFile; } - //============================================================================================== - - - /** - * 以行为单位读取文件,常用于读面向行的格式化文件 - */ - public void readFileByLines(String fileName) { - File file = new File(fileName); - BufferedReader reader = null; - try { - System.out.println("以行为单位读取文件内容,一次读一整行:"); - reader = new BufferedReader(new FileReader(file)); - String tempString = null; - int line = 1; - // 一次读入一行,直到读入null为文件结束 - while ((tempString = reader.readLine()) != null) { - // 显示行号 - System.out.println("line?????????????????????????????????? " + line + ": " + tempString); - String content = tempString; - line++; - } - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e1) { - } - } - } - } + //---------------------------------------------------------------------------------------------- /** * 根据文件路径获取文件 @@ -797,6 +734,7 @@ public void readFileByLines(String fileName) { public static File getFileByPath(String filePath) { return RxDataUtils.isNullString(filePath) ? null : new File(filePath); } + //============================================================================================== /** * 判断文件是否存在 @@ -1876,10 +1814,125 @@ public static Uri getImageContentUri(Context context, java.io.File imageFile) { * @param uri * @return */ - public static File getFileUri(Activity context, Uri uri) { + public static File getFileFromUri(Activity context, Uri uri) { return new File(RxPhotoUtils.getImageAbsolutePath(context, uri)); } + @TargetApi(19) + public static String getPathFromUri(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 the remote address + if (isGooglePhotosUri(uri)) + return uri.getLastPathSegment(); + + return getDataColumn(context, uri, null, null); + } + // File + else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); + } + + return ""; + } + + /** + * @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()); + } + + public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { + Cursor cursor = null; + String column = MediaStore.Images.Media.DATA; + String[] projection = {column}; + try { + cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); + if (cursor != null && cursor.moveToFirst()) { + int index = cursor.getColumnIndexOrThrow(column); + return cursor.getString(index); + } + } finally { + if (cursor != null) + cursor.close(); + } + return null; + } + /** * 安静关闭IO * @@ -1896,6 +1949,66 @@ public static void closeIOQuietly(Closeable... closeables) { } } } + + /** + * 传入文件名以及字符串, 将字符串信息保存到文件中 + * + * @param strFilePath + * @param strBuffer + */ + public void TextToFile(final String strFilePath, final String strBuffer) { + FileWriter fileWriter = null; + try { + // 创建文件对象 + File fileText = new File(strFilePath); + // 向文件写入对象写入信息 + fileWriter = new FileWriter(fileText); + // 写文件 + fileWriter.write(strBuffer); + // 关闭 + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fileWriter.flush(); + fileWriter.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + /** + * 以行为单位读取文件,常用于读面向行的格式化文件 + */ + public void readFileByLines(String fileName) { + File file = new File(fileName); + BufferedReader reader = null; + try { + System.out.println("以行为单位读取文件内容,一次读一整行:"); + reader = new BufferedReader(new FileReader(file)); + String tempString = null; + int line = 1; + // 一次读入一行,直到读入null为文件结束 + while ((tempString = reader.readLine()) != null) { + // 显示行号 + System.out.println("line?????????????????????????????????? " + line + ": " + tempString); + String content = tempString; + line++; + } + + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + } + } + } + } } diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/RxPhotoUtils.java b/RxTools-library/src/main/java/com/vondear/rxtools/RxPhotoUtils.java index 800a783f..412a3d71 100644 --- a/RxTools-library/src/main/java/com/vondear/rxtools/RxPhotoUtils.java +++ b/RxTools-library/src/main/java/com/vondear/rxtools/RxPhotoUtils.java @@ -25,6 +25,12 @@ import java.util.Date; import java.util.Locale; +import static com.vondear.rxtools.RxFileUtils.getDataColumn; +import static com.vondear.rxtools.RxFileUtils.isDownloadsDocument; +import static com.vondear.rxtools.RxFileUtils.isExternalStorageDocument; +import static com.vondear.rxtools.RxFileUtils.isGooglePhotosUri; +import static com.vondear.rxtools.RxFileUtils.isMediaDocument; + /** * Created by vondear on 2016/1/24. */ @@ -258,52 +264,5 @@ else if ("file".equalsIgnoreCase(imageUri.getScheme())) { return null; } - public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { - Cursor cursor = null; - String column = MediaStore.Images.Media.DATA; - String[] projection = {column}; - try { - cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); - if (cursor != null && cursor.moveToFirst()) { - 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()); - } }