Skip to content

Commit

Permalink
完善文件工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
秋逸 committed Apr 19, 2017
1 parent 7bb6d22 commit ae1d1e4
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 116 deletions.
251 changes: 182 additions & 69 deletions RxTools-library/src/main/java/com/vondear/rxtools/RxFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -65,6 +67,7 @@
*/
public class RxFileUtils {

public static final int BUFSIZE = 1024 * 8;
private static final String TAG = "RxFileUtils";

/**
Expand Down Expand Up @@ -255,7 +258,7 @@ public static boolean cleanInternalDbs(Context context) {
* 根据名称清除数据库
* <p>/data/data/com.xxx.xxx/databases/dbName</p>
*
* @param dbName 数据库名称
* @param dbName 数据库名称
* @return {@code true}: 清除成功<br>{@code false}: 清除失败
*/
public static boolean cleanInternalDbByName(Context context, String dbName) {
Expand Down Expand Up @@ -578,7 +581,6 @@ public static String getDiskCacheDir(Context context) {
return cachePath;
}


/**
* 获取缓存视频文件目录
*
Expand All @@ -595,8 +597,6 @@ public static String getDiskFileDir(Context context) {
return cachePath;
}

public static final int BUFSIZE = 1024 * 8;

/**
* 多个文件合并
*
Expand Down Expand Up @@ -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();
}
}
}


//----------------------------------------------------------------------------------------------

/**
* 获取 搜索的路径 下的 所有 后缀 的文件
*
Expand All @@ -752,41 +721,9 @@ public static Vector<String> 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) {
}
}
}
}

//----------------------------------------------------------------------------------------------

/**
* 根据文件路径获取文件
Expand All @@ -797,6 +734,7 @@ public void readFileByLines(String fileName) {
public static File getFileByPath(String filePath) {
return RxDataUtils.isNullString(filePath) ? null : new File(filePath);
}
//==============================================================================================

/**
* 判断文件是否存在
Expand Down Expand Up @@ -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
*
Expand All @@ -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) {
}
}
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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());
}
}

0 comments on commit ae1d1e4

Please sign in to comment.