• Android开发之SDCardUtils工具类。java工具详细代码,附源代码。判断SD卡是否挂载等功能


    package com.xiaobing.zhbj.utils;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import android.os.Environment;
    /**
     * 
     * 
     * @author :QQ:986945193
     * 
     * @新浪微博 :http://weibo.com/mcxiaobing
     * 
     * @version V1.0正式版
     * 
     * SDCard工具类
     *
     */
    public class SDCardUtils {
    	/**
    	 * 判断SD卡是否挂载
    	 * 
    	 * @return
    	 */
    	public static boolean isMounted() {
    		return Environment.getExternalStorageState().equals(
    				Environment.MEDIA_MOUNTED);
    	} 
    
    	// 得到SD卡的根路径
    	public static String getSDPath() {
    
    		if (isMounted()) {
    			return Environment.getExternalStorageDirectory().getAbsolutePath();
    		}
    		return null;
    	}
    
    	// 将文件保存到SD卡中
    	public static boolean saveFileIntoSDCard(byte[] data, String path,
    			String fileName) {
    
    		if (isMounted()) {
    
    			BufferedOutputStream bos = null;
    			try {
    				String filePath = getSDPath() + File.separator + path;
    				File file = new File(filePath);
    				if (!file.exists()) {
    					file.mkdirs();
    				}
    
    				bos = new BufferedOutputStream(new FileOutputStream(new File(
    						file, fileName)));
    				bos.write(data, 0, data.length);
    				bos.flush();
    
    				return true;
    			} catch (Exception e) {
    				e.printStackTrace();
    			} finally {
    				if (bos != null) {
    					try {
    						bos.close();
    					} catch (IOException e1) {
    						e1.printStackTrace();
    					}
    				}
    			}
    
    		}
    
    		return false;
    	}
    
    	// 从SD卡中取出存储的文件
    	public static byte[] getFileFromSDCard(String filePath) {
    
    		if (isMounted()) {
    			File file = new File(filePath);
    			BufferedInputStream bis = null;
    			ByteArrayOutputStream baos = null;
    			if (file.exists()) {
    				try {
    					baos = new ByteArrayOutputStream();
    					bis = new BufferedInputStream(new FileInputStream(file));
    					int len = 0;
    					byte[] buffer = new byte[1024 * 8];
    					while ((len = bis.read(buffer)) != -1) {
    						baos.write(buffer, 0, len);
    						baos.flush();
    					}
    
    					return baos.toByteArray();
    				} catch (Exception e) {
    					e.printStackTrace();
    				} finally {
    					if (bis != null) {
    						try {
    							bis.close();
    							baos.close();
    						} catch (IOException e) {
    							e.printStackTrace();
    						}
    					}
    				}
    
    			}
    		}
    
    		return null;
    
    	}
    }
    

    程序员小冰博客:http://blog.csdn.net/qq_21376985 技术交流QQ986945193 微博:http://weibo.com/mcxiaobing
  • 相关阅读:
    Linux系统模拟端口开放程序port 软件的基本使用
    Docker19.03.13离线安装-Docker根目录-Docker常用操作--NVIDIA Docker
    springboot项目启动与停止命令
    两种设备选型的主要性能指标
    docker nginx配置
    使用shell+java 抓取NHK广播
    小程序的测试方法
    adb logcat 查看Android APP日志
    IE11判断是否全是中文的时候无效写法
    C#控制器微信通过encryptedData,iv,Code获取用户信息
  • 原文地址:https://www.cnblogs.com/mcxiaobing/p/5472065.html
Copyright © 2020-2023  润新知