Android调用系统相机拍照,避免内存溢出的实现

因为调用系统相机拍照的时候,有时候要求连续拍照,而且系统相机的帧数相对较高,所以需要我们事前做好相应的压缩处理,不多说,看代码。


private void intoPhotos() {
    File mediaStorageDir = new File(FileUtil.getPathRoot()+"/ietmDatas","customPicture");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String path1 = mediaStorageDir.getPath();
    String path2 = path1+"/"+timeStamp+".png";
    sdcardTempFile = new File(path2);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    Uri u = Uri.fromFile(sdcardTempFile);
    intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, xiangji);
}


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 3:
                    try {
                        InputStream inputStream = new FileInputStream(sdcardTempFile.getAbsolutePath());
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inTempStorage = new byte[100 * 1024];
                        options.inPreferredConfig = Bitmap.Config.RGB_565;
                        options.inPurgeable = true;
                        options.inSampleSize = 8;
                        options.inInputShareable = true;
                        Bitmap bmp = BitmapFactory.decodeStream(inputStream,null,options);
                        Log.i("wechat", "压缩后图片的大小为"+bmp.getByteCount()/1024+"k宽度为"+bmp.getWidth()+"高度为"+bmp.getHeight());
                        mlist.add(bmp);
                        if (null != mlist && mlist.size()>0){
                            //继续拍照的功能
                            intoPhotos();
                        }
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
            }
        }else if (resultCode == RESULT_PICTOR){
            switch (requestCode){
                case 31:
                    ArrayList bitmaps = data.getStringArrayListExtra("bitmaps");
                    Log.i(TAG, "onActivityResult: "+bitmaps.size()+"");
                    break;
            }
        }
    }

如果有意见和问题,请留言,互相讨论,谢谢?。

你可能感兴趣的:(知识总结)