在開發android 應用的時候我們經常需要用到網路攝影機來進行拍照或者是錄製視頻,這裡介紹一種最簡單的方法:調用系統網路攝影機來拍照,並將照片儲存起來
啟動網路攝影機的方法:
startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);
拍攝成功後接收參數的接收方法我們只需要重寫
protected void onActivityResult(int requestCode, int resultCode, Intent data)方法即可
如:
final int TAKE_PICTURE = 1;protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == TAKE_PICTURE) {if (resultCode == RESULT_OK) {Bitmap bm = (Bitmap) data.getExtras().get("data");imgPhoto.setImageBitmap(bm);// ??¨ª?????¨º??¨²ImageView¨º¨¨ª?¨¦?¡ê?private// ¡ä?¡ä|?D??¡ä?¡ä¡é?¡§¨º?¡¤?¡ä??¨²sdCardExit = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// ImageView img;if (sdCardExit) {myRecAudioDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/custom_picture");if (!myRecAudioDir.exists()) {myRecAudioDir.mkdir();}} else {mMakeTextToast("¡ä?¡ä¡é?¡§2?¡ä??¨²", true);}try {File f = File.createTempFile(strTempFile, ".jpg",myRecAudioDir);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));photoPath=f.getPath();/* 2¨¦¨??1??¡Áa¦Ì¦Ì¡¤?¡¤¡§ */bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);/* ¦Ì¡Â¨?flush()¡¤?¡¤¡§¡ê??¨¹D?BufferStream */bos.flush();/* ?¨¢¨º?OutputStream */bos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}
拍攝效果如:
返回結果如: