文件管理工具類
public final class FileUtil {
//格式化的模板
private static final String TIME_FORMAT = "_yyyyMMdd_HHmmss";
private static final String SDCARD_DIR = Environment.getExternalStorageDirectory().getPath();
private static String getTimeFormatName(String timeFormatHeader) {
final Date date = new Date(System.currentTimeMillis());
//必須要加上單引號
final SimpleDateFormat dateFormat = new SimpleDateFormat("'" + timeFormatHeader + "'" + TIME_FORMAT, Locale.getDefault());
return dateFormat.format(date);
}
/**
* 創建新的圖片文件
*
* @param imageUrl
* @return
*/
public static File getNewFile(String imageUrl) {
File file;
if (TextUtils.isEmpty(imageUrl)) {
file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");//filePath 圖片地址
} else {
file = new File(imageUrl);//filePath 圖片地址
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/**
* @param timeFormatHeader 格式化的頭(除去時間部分)
* @param extension 後綴名
* @return 返回時間格式化後的文件名
*/
public static String getFileNameByTime(String timeFormatHeader, String extension) {
return getTimeFormatName(timeFormatHeader) + "." + extension;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private static File createDir(String sdcardDirName) {
//拼接成SD卡中完整的dir
final String dir = SDCARD_DIR + "/" + sdcardDirName + "/";
final File fileDir = new File(dir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
return fileDir;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static File createFile(String sdcardDirName, String fileName) {
return new File(createDir(sdcardDirName), fileName);
}
private static File createFileByTime(String sdcardDirName, String timeFormatHeader, String extension) {
final String fileName = getFileNameByTime(timeFormatHeader, extension);
return createFile(sdcardDirName, fileName);
}
//獲取文件的MIME
public static String getMimeType(String filePath) {
final String extension = getExtension(filePath);
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
//獲取文件的後綴名
public static String getExtension(String filePath) {
String suffix = "";
final File file = new File(filePath);
final String name = file.getName();
final int idx = name.lastIndexOf('.');
if (idx > 0) {
suffix = name.substring(idx + 1);
}
return suffix;
}
/**
* 保存Bitmap到SD卡中
*
* @param dir 目錄名,只需要寫自己的相對目錄名即可
* @param compress 壓縮比例 100是不壓縮,值約小壓縮率越高
* @return 返回該文件
*/
public static File saveBitmap(Bitmap mBitmap, String dir, int compress) {
final String sdStatus = Environment.getExternalStorageState();
// 檢測sd是否可用
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
return null;
}
FileOutputStream fos = null;
BufferedOutputStream bos = null;
File fileName = createFileByTime(dir, "3rde", "jpg");
try {
fos = new FileOutputStream(fileName);
bos = new BufferedOutputStream(fos);
mBitmap.compress(Bitmap.CompressFormat.JPEG, compress, bos);// 把數據寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.flush();
}
if (bos != null) {
bos.close();
}
//關閉流
if (fos != null) {
fos.flush();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
refreshDCIM();
return fileName;
}
public static File writeToDisk(InputStream is, String dir, String name) {
final File file = FileUtil.createFile(dir, name);
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte data[] = new byte[1024 * 4];
int count;
while ((count = bis.read(data)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static File writeToDisk(InputStream is, String dir, String prefix, String extension) {
final File file = FileUtil.createFileByTime(dir, prefix, extension);
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte data[] = new byte[1024 * 4];
int count;
while ((count = bis.read(data)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/**
* 通知系統刷新系統相冊,使照片展現出來
*/
private static void refreshDCIM() {
if (Build.VERSION.SDK_INT >= 19) {
//兼容android4.4版本,只掃描存放照片的目錄
MediaScannerConnection.scanFile(BaseApplication.getContext(),
new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()},
null, null);
} else {
//掃描整個SD卡來更新系統圖庫,當文件很多時用戶體驗不佳,且不適合4.4以上版本
BaseApplication.getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +
Environment.getExternalStorageDirectory())));
}
}
/**
* 讀取raw目錄中的文件,並返回爲字符串
*/
public static String getRawFile(int id) {
final InputStream is = BaseApplication.getContext().getResources().openRawResource(id);
final BufferedInputStream bis = new BufferedInputStream(is);
final InputStreamReader isr = new InputStreamReader(bis);
final BufferedReader br = new BufferedReader(isr);
final StringBuilder stringBuilder = new StringBuilder();
String str;
try {
while ((str = br.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
isr.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return stringBuilder.toString();
}
public static void setIconFont(String path, TextView textView) {
final Typeface typeface = Typeface.createFromAsset(BaseApplication.getContext().getAssets(), path);
textView.setTypeface(typeface);
}
/**
* 讀取assets目錄下的文件,並返回字符串
*/
public static String getAssetsFile(String name) {
InputStream is = null;
BufferedInputStream bis = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuilder stringBuilder = null;
final AssetManager assetManager = BaseApplication.getContext().getAssets();
try {
is = assetManager.open(name);
bis = new BufferedInputStream(is);
isr = new InputStreamReader(bis);
br = new BufferedReader(isr);
stringBuilder = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
assetManager.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (stringBuilder != null) {
return stringBuilder.toString();
} else {
return null;
}
}
/**
* 判斷SD卡是否可用
*
* @return SD卡可用返回true
*/
public static boolean hasSdcard() {
String status = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(status);
}
public static String getRealFilePath(final Context context, final Uri uri) {
if (null == uri) return null;
final String scheme = uri.getScheme();
String data = null;
if (scheme == null)
data = uri.getPath();
else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
final Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
final int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if (index > -1) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
}
/**
* 相機拍照保存的文件全路徑
*/
public static File getTakePhotoFile() {
File file = null;
try {
file = new File(Environment.getExternalStorageDirectory(), "takephoto.jpg");
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 相機拍照保存的文件全路徑(裁剪後的臨時路徑)
*/
public static File getTempPhotoFile() {
File file = null;
try {
file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 相機拍照保存的文件全路徑
*/
public static String getTakePhotoFileDirectory() {
String dirs = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
File fileDirectory = new File(dirs);
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
return dirs;
}
/**
* 創建文件
*
* @param path
* @return
*/
public static File createDirFile(String path) {
int pos = path.lastIndexOf("/");
String dirpath = path.substring(0, pos + 1);
if (!dirpath.startsWith("/"))
dirpath = "/" + dirpath;
File f = new File(dirpath);
if (!f.exists())
f.mkdirs();
return new File(path);
}
/**
* 通過文件名,判斷指定目錄下是否有改文件
*
* @param filename
*/
public static File isExitByFileName(String path, String filename) {
File isExitFile = null;
File dir = new File(path);
File[] listFiles = dir.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (file.getName().equals(filename)) {
isExitFile = file;
break;
}
}
}
return isExitFile;
}
/**
* 輸入流轉文件並保存到本地
*
* @param is 文件輸入流
* @param savePath 保存的路徑
* @param fileName 文件名
* @return
*/
public static File InputStreamToFile(InputStream is, String savePath, String fileName) {
try {
File dir = new File(savePath);
if (!dir.exists()) {
dir.mkdirs();
}
File outFile = new File(dir + "/" + fileName);
if (outFile != null) {
FileOutputStream fos = new FileOutputStream(outFile);
byte[] buffer = new byte[1024 * 10];
int count = 0;
while ((count = is.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
is.close();
fos.flush();
fos.close();
return outFile;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* create the targer file's dir
*
* @param path the file's absolute path
*/
public static void createDirs(String path) {
path = path.substring(0, path.lastIndexOf("/") + 1);
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
}
/***
* 刪除指定目錄下得所有文件
* @param dirPath
*/
public static boolean clearAllFile(String dirPath) {
File file = new File(dirPath);
if (file != null && file.exists()) {
if (file.isFile()) {
return file.delete();
} else if (file.isDirectory()) {
File[] listFiles = file.listFiles();
for (int i = 0; listFiles != null && i < listFiles.length; i++) {
if (listFiles[i].isFile()) {
if (!listFiles[i].delete()) {
LogUtils.d("Delete file failed!" + dirPath);
return false;
}
} else {
if (!clearAllFile(listFiles[i].getPath())) {
return false;
}
}
}
}
}
LogUtils.d("Delete file----file is not exists----" + dirPath);
return true;
}
/*****
* 刪除指定路徑APK文件
* @param path
*/
public static void rmoveAPKFile(String path) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), path.substring(path.lastIndexOf("/")));
if (file.exists()) {
file.delete();
}
if (!file.exists())
LogUtils.d("安裝文件刪除成功");
}
/**
* 創建目錄
*
* @param path
* @return
*/
public static File createDirByPath(String path) {
File file = new File(path);
if (file != null && !file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 複製文件到指定目錄
*
* @param is
* @param dirPath
* @param fileName
*/
public static File copyFile(InputStream is, String dirPath, String fileName) {
try {
if (is != null) {
File file = new File(createDirByPath(dirPath), fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 100];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.flush();
fos.close();
return file;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
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());
}
/*******
* 相冊選取圖片得到的Uri是Content Uri而拍照後使用的是文件路徑生成的File Uri,
* 看來問題就是出在這裏,並不是說我們app的targetSDKVersion不是24就可以使用File Uri,
* 但是photos app的targetSdkVersion可能是24導致了它接受了File Uri而崩潰,
* 那麼我們需要做的就是把File Uri換成Content Uri。這裏需要提的是,
* 直接按照這裏的做法去更換Content Uri並不能生效,會提示“Can not edit image under 50*50 pixels”的錯誤toast提示,
* 其實是photos app找不到Content Uri傳進去的圖片文件。那麼我們需要換一種方式去更換Content Uri,
* 我們在stackoverflow上面找到更換Content Uri的方法,需要注意的是不是所有的File Uri都可以轉換成Content Uri,應該是多媒體相關的文件纔可以。
* @return
* 參考 http://www.jianshu.com/p/c73b959b6bcf
*/
public static Uri getImageContentUri(String filePath) {
Cursor cursor = BaseApplication.getContext().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (new File(filePath).exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return BaseApplication.getContext().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
/****
* 讀取Asset中的文件
* @param assetManager
* @param path
* @return
*/
public static String getAssetFileToStr(AssetManager assetManager, String path) {
BufferedReader bufferedReader = null;
StringBuilder result = new StringBuilder();
try {
bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open(path), "UTF-8"));
for (String ignored = null; (ignored = bufferedReader.readLine()) != null; ) {
result.append(ignored);
}
} catch (IOException var13) {
return "";
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException var12) {
;
}
}
}
return result.toString().trim();
}
/******
* 根據URI獲取文件絕對路徑
* @param contentUri
* @return
* liukui 2017/05/19
*/
public static String getRealPathFromURI(Uri contentUri) {
if (contentUri == null) return null;
int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion < 11) {
// SDK < Api11
return getRealPathFromUri_BelowApi11(contentUri);
}
if (sdkVersion < 19) {
// SDK > 11 && SDK < 19
return getRealPathFromUri_Api11To18(contentUri);
}
// SDK > 19
return getRealPathFromUri_AboveApi19(contentUri);
}
/**
* 適配api19以上,根據uri獲取圖片的絕對路徑
* liukui 2017/05/19
*/
@SuppressLint("NewApi")
private static String getRealPathFromUri_AboveApi19(Uri uri) {
if (DocumentsContract.isDocumentUri(BaseApplication.getInstance(), uri)) { // 【DocumentProvider】
if (isExternalStorageDocument(uri)) { // 【ExternalStorageProvider】
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type))
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) { // 【DownloadsProvider】
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(contentUri, null, null);
} else if (isMediaDocument(uri)) { // 【MediaProvider】
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
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;
String selection = "_id=?";
String[] selectionArgs = new String[]{split[1]};
return getDataColumn(contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) { // 【MediaStore (and general)】
return getDataColumn(uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) { // 【File】
return uri.getPath();
}
return "";
}
/**
* Get the value of the data column for this Uri. This is useful for MediaStore Uris, and other file-based ContentProviders.
*
* @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(Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
String result = "";
cursor = BaseApplication.getContext().getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if (index > -1) {
result = cursor.getString(index);
}
}
cursor.close();
}
return result;
}
/**
* 適配api11-api18,根據uri獲取圖片的絕對路徑
* liukui 2017/05/19
*/
private static String getRealPathFromUri_Api11To18(Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(BaseApplication.getInstance(), uri, projection, null,
null, null);
Cursor cursor = loader.loadInBackground();
if (cursor == null)
return "";
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
return filePath;
}
/**
* 適配api11以下(不包括api11),根據uri獲取圖片的絕對路徑
* liukui 2017/05/19
*/
private static String getRealPathFromUri_BelowApi11(Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = BaseApplication.getInstance().getContentResolver().query(uri, projection,
null, null, null);
if (cursor == null)
return "";
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
return filePath;
}
public static void DeleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
if (file.getAbsoluteFile().delete()) {
LogUtils.d(file.getName() + " 刪除成功!");
}
}
}
/**
* 打開文件
* 兼容7.0
*
* @param context activity
* @param file File
* @param contentType 文件類型如:文本(text/html)
* 當手機中沒有一個app可以打開file時會拋ActivityNotFoundException
*/
public static void startActionFile(Context context, File file, String contentType) throws ActivityNotFoundException {
if (context == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加讀寫權限
intent.setDataAndType(getUriFromFile(), contentType);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
}
public static Uri getUriFromFile() {
File file = getTakePhotoFile();
if (file == null) {
throw new NullPointerException();
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//uri = FileProvider.getUriForFile(BaseApplication.getInstance(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
String pageName = BaseApplication.getInstance().getAppFrom().equals(BaseStringUtils.AppFromParent) ? "com.shengzhe.disan.xuetangparent.fileprovider" : "com.shengzhe.disan.xuetangteacher.fileprovider";
uri = FileProvider.getUriForFile(BaseApplication.getInstance(), pageName, file);
} else {
uri = Uri.fromFile(file);
}
return uri;
}
//格式化的模板
private static final String TIME_FORMAT = "_yyyyMMdd_HHmmss";
private static final String SDCARD_DIR = Environment.getExternalStorageDirectory().getPath();
private static String getTimeFormatName(String timeFormatHeader) {
final Date date = new Date(System.currentTimeMillis());
//必須要加上單引號
final SimpleDateFormat dateFormat = new SimpleDateFormat("'" + timeFormatHeader + "'" + TIME_FORMAT, Locale.getDefault());
return dateFormat.format(date);
}
/**
* 創建新的圖片文件
*
* @param imageUrl
* @return
*/
public static File getNewFile(String imageUrl) {
File file;
if (TextUtils.isEmpty(imageUrl)) {
file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");//filePath 圖片地址
} else {
file = new File(imageUrl);//filePath 圖片地址
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/**
* @param timeFormatHeader 格式化的頭(除去時間部分)
* @param extension 後綴名
* @return 返回時間格式化後的文件名
*/
public static String getFileNameByTime(String timeFormatHeader, String extension) {
return getTimeFormatName(timeFormatHeader) + "." + extension;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private static File createDir(String sdcardDirName) {
//拼接成SD卡中完整的dir
final String dir = SDCARD_DIR + "/" + sdcardDirName + "/";
final File fileDir = new File(dir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
return fileDir;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static File createFile(String sdcardDirName, String fileName) {
return new File(createDir(sdcardDirName), fileName);
}
private static File createFileByTime(String sdcardDirName, String timeFormatHeader, String extension) {
final String fileName = getFileNameByTime(timeFormatHeader, extension);
return createFile(sdcardDirName, fileName);
}
//獲取文件的MIME
public static String getMimeType(String filePath) {
final String extension = getExtension(filePath);
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
//獲取文件的後綴名
public static String getExtension(String filePath) {
String suffix = "";
final File file = new File(filePath);
final String name = file.getName();
final int idx = name.lastIndexOf('.');
if (idx > 0) {
suffix = name.substring(idx + 1);
}
return suffix;
}
/**
* 保存Bitmap到SD卡中
*
* @param dir 目錄名,只需要寫自己的相對目錄名即可
* @param compress 壓縮比例 100是不壓縮,值約小壓縮率越高
* @return 返回該文件
*/
public static File saveBitmap(Bitmap mBitmap, String dir, int compress) {
final String sdStatus = Environment.getExternalStorageState();
// 檢測sd是否可用
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
return null;
}
FileOutputStream fos = null;
BufferedOutputStream bos = null;
File fileName = createFileByTime(dir, "3rde", "jpg");
try {
fos = new FileOutputStream(fileName);
bos = new BufferedOutputStream(fos);
mBitmap.compress(Bitmap.CompressFormat.JPEG, compress, bos);// 把數據寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.flush();
}
if (bos != null) {
bos.close();
}
//關閉流
if (fos != null) {
fos.flush();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
refreshDCIM();
return fileName;
}
public static File writeToDisk(InputStream is, String dir, String name) {
final File file = FileUtil.createFile(dir, name);
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte data[] = new byte[1024 * 4];
int count;
while ((count = bis.read(data)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
public static File writeToDisk(InputStream is, String dir, String prefix, String extension) {
final File file = FileUtil.createFileByTime(dir, prefix, extension);
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte data[] = new byte[1024 * 4];
int count;
while ((count = bis.read(data)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/**
* 通知系統刷新系統相冊,使照片展現出來
*/
private static void refreshDCIM() {
if (Build.VERSION.SDK_INT >= 19) {
//兼容android4.4版本,只掃描存放照片的目錄
MediaScannerConnection.scanFile(BaseApplication.getContext(),
new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()},
null, null);
} else {
//掃描整個SD卡來更新系統圖庫,當文件很多時用戶體驗不佳,且不適合4.4以上版本
BaseApplication.getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +
Environment.getExternalStorageDirectory())));
}
}
/**
* 讀取raw目錄中的文件,並返回爲字符串
*/
public static String getRawFile(int id) {
final InputStream is = BaseApplication.getContext().getResources().openRawResource(id);
final BufferedInputStream bis = new BufferedInputStream(is);
final InputStreamReader isr = new InputStreamReader(bis);
final BufferedReader br = new BufferedReader(isr);
final StringBuilder stringBuilder = new StringBuilder();
String str;
try {
while ((str = br.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
isr.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return stringBuilder.toString();
}
public static void setIconFont(String path, TextView textView) {
final Typeface typeface = Typeface.createFromAsset(BaseApplication.getContext().getAssets(), path);
textView.setTypeface(typeface);
}
/**
* 讀取assets目錄下的文件,並返回字符串
*/
public static String getAssetsFile(String name) {
InputStream is = null;
BufferedInputStream bis = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuilder stringBuilder = null;
final AssetManager assetManager = BaseApplication.getContext().getAssets();
try {
is = assetManager.open(name);
bis = new BufferedInputStream(is);
isr = new InputStreamReader(bis);
br = new BufferedReader(isr);
stringBuilder = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
assetManager.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (stringBuilder != null) {
return stringBuilder.toString();
} else {
return null;
}
}
/**
* 判斷SD卡是否可用
*
* @return SD卡可用返回true
*/
public static boolean hasSdcard() {
String status = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(status);
}
public static String getRealFilePath(final Context context, final Uri uri) {
if (null == uri) return null;
final String scheme = uri.getScheme();
String data = null;
if (scheme == null)
data = uri.getPath();
else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
data = uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
final Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
final int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if (index > -1) {
data = cursor.getString(index);
}
}
cursor.close();
}
}
return data;
}
/**
* 相機拍照保存的文件全路徑
*/
public static File getTakePhotoFile() {
File file = null;
try {
file = new File(Environment.getExternalStorageDirectory(), "takephoto.jpg");
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 相機拍照保存的文件全路徑(裁剪後的臨時路徑)
*/
public static File getTempPhotoFile() {
File file = null;
try {
file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 相機拍照保存的文件全路徑
*/
public static String getTakePhotoFileDirectory() {
String dirs = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
File fileDirectory = new File(dirs);
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
return dirs;
}
/**
* 創建文件
*
* @param path
* @return
*/
public static File createDirFile(String path) {
int pos = path.lastIndexOf("/");
String dirpath = path.substring(0, pos + 1);
if (!dirpath.startsWith("/"))
dirpath = "/" + dirpath;
File f = new File(dirpath);
if (!f.exists())
f.mkdirs();
return new File(path);
}
/**
* 通過文件名,判斷指定目錄下是否有改文件
*
* @param filename
*/
public static File isExitByFileName(String path, String filename) {
File isExitFile = null;
File dir = new File(path);
File[] listFiles = dir.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (file.getName().equals(filename)) {
isExitFile = file;
break;
}
}
}
return isExitFile;
}
/**
* 輸入流轉文件並保存到本地
*
* @param is 文件輸入流
* @param savePath 保存的路徑
* @param fileName 文件名
* @return
*/
public static File InputStreamToFile(InputStream is, String savePath, String fileName) {
try {
File dir = new File(savePath);
if (!dir.exists()) {
dir.mkdirs();
}
File outFile = new File(dir + "/" + fileName);
if (outFile != null) {
FileOutputStream fos = new FileOutputStream(outFile);
byte[] buffer = new byte[1024 * 10];
int count = 0;
while ((count = is.read(buffer)) != -1) {
fos.write(buffer, 0, count);
}
is.close();
fos.flush();
fos.close();
return outFile;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* create the targer file's dir
*
* @param path the file's absolute path
*/
public static void createDirs(String path) {
path = path.substring(0, path.lastIndexOf("/") + 1);
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
}
/***
* 刪除指定目錄下得所有文件
* @param dirPath
*/
public static boolean clearAllFile(String dirPath) {
File file = new File(dirPath);
if (file != null && file.exists()) {
if (file.isFile()) {
return file.delete();
} else if (file.isDirectory()) {
File[] listFiles = file.listFiles();
for (int i = 0; listFiles != null && i < listFiles.length; i++) {
if (listFiles[i].isFile()) {
if (!listFiles[i].delete()) {
LogUtils.d("Delete file failed!" + dirPath);
return false;
}
} else {
if (!clearAllFile(listFiles[i].getPath())) {
return false;
}
}
}
}
}
LogUtils.d("Delete file----file is not exists----" + dirPath);
return true;
}
/*****
* 刪除指定路徑APK文件
* @param path
*/
public static void rmoveAPKFile(String path) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), path.substring(path.lastIndexOf("/")));
if (file.exists()) {
file.delete();
}
if (!file.exists())
LogUtils.d("安裝文件刪除成功");
}
/**
* 創建目錄
*
* @param path
* @return
*/
public static File createDirByPath(String path) {
File file = new File(path);
if (file != null && !file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 複製文件到指定目錄
*
* @param is
* @param dirPath
* @param fileName
*/
public static File copyFile(InputStream is, String dirPath, String fileName) {
try {
if (is != null) {
File file = new File(createDirByPath(dirPath), fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 100];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.flush();
fos.close();
return file;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
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());
}
/*******
* 相冊選取圖片得到的Uri是Content Uri而拍照後使用的是文件路徑生成的File Uri,
* 看來問題就是出在這裏,並不是說我們app的targetSDKVersion不是24就可以使用File Uri,
* 但是photos app的targetSdkVersion可能是24導致了它接受了File Uri而崩潰,
* 那麼我們需要做的就是把File Uri換成Content Uri。這裏需要提的是,
* 直接按照這裏的做法去更換Content Uri並不能生效,會提示“Can not edit image under 50*50 pixels”的錯誤toast提示,
* 其實是photos app找不到Content Uri傳進去的圖片文件。那麼我們需要換一種方式去更換Content Uri,
* 我們在stackoverflow上面找到更換Content Uri的方法,需要注意的是不是所有的File Uri都可以轉換成Content Uri,應該是多媒體相關的文件纔可以。
* @return
* 參考 http://www.jianshu.com/p/c73b959b6bcf
*/
public static Uri getImageContentUri(String filePath) {
Cursor cursor = BaseApplication.getContext().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (new File(filePath).exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return BaseApplication.getContext().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
/****
* 讀取Asset中的文件
* @param assetManager
* @param path
* @return
*/
public static String getAssetFileToStr(AssetManager assetManager, String path) {
BufferedReader bufferedReader = null;
StringBuilder result = new StringBuilder();
try {
bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open(path), "UTF-8"));
for (String ignored = null; (ignored = bufferedReader.readLine()) != null; ) {
result.append(ignored);
}
} catch (IOException var13) {
return "";
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException var12) {
;
}
}
}
return result.toString().trim();
}
/******
* 根據URI獲取文件絕對路徑
* @param contentUri
* @return
* liukui 2017/05/19
*/
public static String getRealPathFromURI(Uri contentUri) {
if (contentUri == null) return null;
int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion < 11) {
// SDK < Api11
return getRealPathFromUri_BelowApi11(contentUri);
}
if (sdkVersion < 19) {
// SDK > 11 && SDK < 19
return getRealPathFromUri_Api11To18(contentUri);
}
// SDK > 19
return getRealPathFromUri_AboveApi19(contentUri);
}
/**
* 適配api19以上,根據uri獲取圖片的絕對路徑
* liukui 2017/05/19
*/
@SuppressLint("NewApi")
private static String getRealPathFromUri_AboveApi19(Uri uri) {
if (DocumentsContract.isDocumentUri(BaseApplication.getInstance(), uri)) { // 【DocumentProvider】
if (isExternalStorageDocument(uri)) { // 【ExternalStorageProvider】
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type))
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) { // 【DownloadsProvider】
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(contentUri, null, null);
} else if (isMediaDocument(uri)) { // 【MediaProvider】
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
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;
String selection = "_id=?";
String[] selectionArgs = new String[]{split[1]};
return getDataColumn(contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) { // 【MediaStore (and general)】
return getDataColumn(uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) { // 【File】
return uri.getPath();
}
return "";
}
/**
* Get the value of the data column for this Uri. This is useful for MediaStore Uris, and other file-based ContentProviders.
*
* @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(Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
String result = "";
cursor = BaseApplication.getContext().getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
if (index > -1) {
result = cursor.getString(index);
}
}
cursor.close();
}
return result;
}
/**
* 適配api11-api18,根據uri獲取圖片的絕對路徑
* liukui 2017/05/19
*/
private static String getRealPathFromUri_Api11To18(Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(BaseApplication.getInstance(), uri, projection, null,
null, null);
Cursor cursor = loader.loadInBackground();
if (cursor == null)
return "";
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
return filePath;
}
/**
* 適配api11以下(不包括api11),根據uri獲取圖片的絕對路徑
* liukui 2017/05/19
*/
private static String getRealPathFromUri_BelowApi11(Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = BaseApplication.getInstance().getContentResolver().query(uri, projection,
null, null, null);
if (cursor == null)
return "";
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
return filePath;
}
public static void DeleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
if (file.getAbsoluteFile().delete()) {
LogUtils.d(file.getName() + " 刪除成功!");
}
}
}
/**
* 打開文件
* 兼容7.0
*
* @param context activity
* @param file File
* @param contentType 文件類型如:文本(text/html)
* 當手機中沒有一個app可以打開file時會拋ActivityNotFoundException
*/
public static void startActionFile(Context context, File file, String contentType) throws ActivityNotFoundException {
if (context == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加讀寫權限
intent.setDataAndType(getUriFromFile(), contentType);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
}
public static Uri getUriFromFile() {
File file = getTakePhotoFile();
if (file == null) {
throw new NullPointerException();
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//uri = FileProvider.getUriForFile(BaseApplication.getInstance(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
String pageName = BaseApplication.getInstance().getAppFrom().equals(BaseStringUtils.AppFromParent) ? "com.shengzhe.disan.xuetangparent.fileprovider" : "com.shengzhe.disan.xuetangteacher.fileprovider";
uri = FileProvider.getUriForFile(BaseApplication.getInstance(), pageName, file);
} else {
uri = Uri.fromFile(file);
}
return uri;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.