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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hejunw/article/details/81537429
//     给定一个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();
        }

    }

猜你喜欢

转载自blog.csdn.net/hejunw/article/details/81537429