FileUtil 项目标准写法

	public final class FileUtil {
	
	    //默认本地上传图片目录
	    public static final String UPLOAD_PHOTO_DIR =
	            Environment.getExternalStorageDirectory().getPath() + "/a_upload_photos/";
	    //网页缓存地址
	    public static final String WEB_CACHE_DIR =
	            Environment.getExternalStorageDirectory().getPath() + "/app_web_cache/";
	    //系统相机目录
	    public static final String CAMERA_PHOTO_DIR =
	            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/Camera/";
	    //格式化的模板
	    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 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, "DOWN_LOAD", "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(Milk.getApplicationContext(),
	                    new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()},
	                    null, null);
	        } else {
	            //扫描整个SD卡来更新系统图库,当文件很多时用户体验不佳,且不适合4.4以上版本
	            Milk.getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +
	                    Environment.getExternalStorageDirectory())));
	        }
	    }
	
	    /**
	     * 读取raw目录中的文件,并返回为字符串
	     */
	    public static String getRawFile(int id) {
	        final InputStream is = Milk.getApplicationContext().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(Milk.getApplicationContext().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 = Milk.getApplicationContext().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;
	        }
	    }
	
	    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;
	    }
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值