指定存储位置/Bitmap内存溢出解决/解决Bitmap无法序列化问题/输入流转Bitmap

博客探讨了Bitmap在Android开发中的常见问题,包括如何利用输入流存储Bitmap以避免内存溢出,解决Bitmap无法序列化的问题,以及在SharedPreference中的存储和读取策略。文章提到了将Bitmap转换为字节数组作为替代方案,并提醒开发者注意mkdir()和mkdirs()的区别,以正确创建目录。
摘要由CSDN通过智能技术生成

输入流本地存储

外置存储卡存储

一般情况下小文件图片之类的不建议存在外置存储存在SharedPreference当中较好

 VacationService vacationService= HttpRPC.getService(VacationService.class, AppUtil.getYzamsAddr());
        InputStream result= vacationService.downloadSickCertificate(certificatePicID);
        if (result == null) {
            throw new RuntimeException("stream is null");
        } else {

            try {
                File file= new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
                if(!file.exists()){
                    file.createNewFile();}
                LogUtil.e(".............0.0.0.."+Environment.getExternalStorageDirectory());
                AppUtil.setPicTimeMillersName(file.getName());
                LogUtil.e("................."+file.getName());
                FileOutputStream fileOutputStream=new FileOutputStream(file);

                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[10240];
                int len = 0;
                while ((len = result.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                    fileOutputStream.write(buffer);
                }

                byte[] data=outStream.toByteArray();
                if (data != null) {
                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                }
                outStream.close();
                result.close();
            } catch (Exception e) {
                e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值