Android 调用系统的API拍照,在图片库中显示

本文介绍了一种将Bitmap对象保存为JPEG格式图片的方法,并详细解释了如何根据计数选择存储路径,以及如何利用Android系统API完成图片的压缩、保存及更新系统相册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//     给定一个Bitmap,进行保存
    public void saveJpeg(Bitmap bm) {
        String savePath = null;
        if (intcount <= 1500) {
            savePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "DCIM/Camera";
            Log.e(TAG, "save bitmap的存储路径是 :" + savePath + "," + "incount的值是:" + intcount);
            File folder = new File(savePath);
            if (!folder.exists()) // 如果文件夹不存在则创建
            {
                folder.mkdir();
            }
        } else if (intcount >= 1501) {
            savePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "DCIM/Camera"
                    + String.valueOf(1000);
            Log.e(TAG, "save bitmap的存储路径是 :" + savePath + "," + "incount的值是:" + intcount);
            File folder2 = new File(savePath);
            if (!folder2.exists()) // 如果文件夹不存在则创建
            {
                folder2.mkdir();
            }


        }
        long dataTake = System.currentTimeMillis();
        String jpegName = savePath + File.separator +"IMG_"+ dataTake +"_"+intcount+"_"+ ".jpg";
        Log.e(TAG, "saveJpeg:jpegName--" + jpegName);
        File jpegFile = new File(jpegName);
        try {
            FileOutputStream fout = new FileOutputStream(jpegName);
            BufferedOutputStream bos = new BufferedOutputStream(fout);
            // TODO: 2018/8/8  100就表示 不压缩 
            bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);

            // TODO: 2018/8/9 保存到图片库 ,可以看到
            MediaStore.Images.Media.insertImage(CameraAct.this.getContentResolver(),
                    jpegFile.getAbsolutePath(), jpegName, null);
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(new File(jpegName));
            System.out.println(jpegName+"uriuri:::"+uri);
            intent.setData(uri);
            sendBroadcast(intent);

            bos.flush();
            bos.close();
            mCamera.stopPreview();
            mCamera.startPreview();
            bm.recycle();


            // succCount = succCount + 1;
            Log.e(TAG, "saveJpeg:存储完毕!-->" + succCount);
        } catch (IOException e) {


            Log.e(TAG, "saveJpeg:存储失败!-->" + failCount);
            e.printStackTrace();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值