使用 Intent 啟動 android 手機預設的相機

使用 Intent 啟動 android 手機預設的相機

    <uses -feature android:name="android.hardware.camera" android:required="true"></uses>
`</pre>

<pre>`    static final int SNAPSHOT = 1;

    public void testClick( View view ){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(cameraIntent, SNAPSHOT);
        }
    }
`</pre>

接收 MediaStore.ACTION_IMAGE_CAPTURE Intent 回傳的結果

<pre>`    &lt;uses -permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses&gt;
`</pre>

<pre>`    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK &amp;&amp; requestCode == SNAPSHOT) {
            Bundle extras = data.getExtras();
            Bitmap cameraBMP = (Bitmap) extras.get("data");
            ImageView iv;
            iv = (ImageView)findViewById( R.id.myimg );
            iv.setImageBitmap(cameraBMP);
        }
    }