Androidはシステムカメラを呼び出して撮影し、ローカルに保存します.

6393 ワード

public class PhotographActivity extends AppCompatActivity {

    public static Handler handler ;
    private static String srcPath;
    private String fileNmae;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photograph);
        String name = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        fileNmae = Environment.getExternalStorageDirectory().toString()+File.separator+"OCR/image/"+name+".jpg";
        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileNmae)));
            startActivityForResult(it, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Bitmap b = BitmapFactory.decodeFile(fileNmae);
            //        
            srcPath = fileNmae;
            Message msg = Message.obtain();
            msg.obj=srcPath;
            if (handler!=null){
                handler.sendMessage(msg);
            }
            File myCaptureFile =new File(fileNmae);
            try {
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    if(!myCaptureFile.getParentFile().exists()){
                        myCaptureFile.getParentFile().mkdirs();
                    }
                    BufferedOutputStream bos;
                    bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
                    b.compress(Bitmap.CompressFormat.JPEG, 60, bos);
                    bos.flush();
                    bos.close();
                }else{
                    Toast toast= Toast.makeText(this, "SD   ", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        finish();
    }

}