Android 读取内存文件返回byte数组

File file = new File(Environment.getExternalStorageDirectory()+"/w650.jpg");
 
private byte[] readFile(File file) {
    // 需要读取的文件,参数是文件的路径名加文件名
    if (file.isFile()) {
        // 以字节流方法读取文件

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            // 设置一个,每次 装载信息的容器
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            // 开始读取数据
            int len = 0;// 每次读取到的数据的长度
            while ((len = fis.read(buffer)) != -1) {// len值为-1时,表示没有数据了
                // append方法往sb对象里面添加数据
                outputStream.write(buffer, 0, len);
            }
            // 输出字符串
            return outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("文件不存在!");
    }
    return null;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值