首页 > 代码库 > 拍照并保存到指定文件夹

拍照并保存到指定文件夹

关键代码:

protected void takePhoto(View v){
        if( v.getId() == R.id.btn ) {
            File dir = new File(Environment.getExternalStorageDirectory(),"Guo");
            if(!dir.exists()){
                dir.mkdir();
            }

            curFile = new File(dir, System.currentTimeMillis() + ".jpg");
            if( !curFile.exists() ){
                try{
                    curFile.createNewFile();
                }catch (IOException e){
                    e.printStackTrace();
                }

            }
            Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(curFile));
            startActivityForResult(it, TAKE_PHOTO);
        }
}

 

 

处理相机返回数据,即图片数据:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode){
            case TAKE_PHOTO:
                if(resultCode == RESULT_OK){
                    //picture.setImageURI(Uri.fromFile(curFile));
                    int degree = readPictureDegree(curFile.getAbsolutePath());
                    BitmapFactory.Options opts=new BitmapFactory.Options();
                    opts.inSampleSize=2;
                    Bitmap cbitmap = BitmapFactory.decodeFile(curFile.getAbsolutePath(),opts);
                    Bitmap bitmap = rotaingImageView(degree, cbitmap);
           sysImageNotify();
            picture.setImageBitmap(bitmap); 
          }
          break;
    }
}

 

照相机拍照后,有些图片是获取图片的旋转角度

// 获取旋转角度
public  int readPictureDegree(String path) {
        int degree  = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

 

  

旋转图片:

/*
    * 旋转图片
    */
    public  Bitmap rotaingImageView(int angle , Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();;
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;
    }

 

 

图片保存进入文件夹后,在系统图库中显示

protected void sysImageNotify(){
	// 文件插入系统图库
        try {
            MediaStore.Images.Media.insertImage(
                    getContentResolver(),
                    curFile.getAbsolutePath(), curFile.getName(), null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        sendBroadcast(
                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())
            )
        );

 }

  

 所需权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>

 

拍照并保存到指定文件夹