這在android中要怎麽做呢?查了一下分成兩個部分:
第一個部分是呼叫照相機
public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1234567; ... mBtnTakePicture.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { //define the file-name to save photo taken by Camera activity String fileName = "new-photo-name.jpg"; //create parameters for Intent with filename ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, fileName); values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera"); //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState) imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); //create new Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); }
其中重要的是要定義一個辨識碼(CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE),在呼叫startActivityForResult中,將辨識碼傳入,這是為了返回時用來辨識的。
第二步當照相機結束返回時
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { //use imageUri here to access the image Cursor cursor = null; try { String [] proj={ MediaStore.Images.Media.DATA }; //, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION }; cursor = RecordView.this.managedQuery(imageUri, proj, // Which columns to return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) null); // Order-by clause (ascending by name) int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); //int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION); if (cursor.moveToFirst()) { String imagePath = cursor.getString(file_ColumnIndex); .... } finally { if (cursor != null) { cursor.close(); } }
先判斷返回碼是正確的,再確認結果是正確的(表示使用者有拍照並存儲),然後利用managedQuery讀出cursor,就可以取出照片在存儲媒體中的位置了。
您好!
回覆刪除謝謝您的教學~很受用^^
想請問您以這個方式拍到的照片
要怎麼從sd卡中刪除呢?
我試過用File去讀imagePath
再用delete()去刪除
但好像失敗了
請大師賜教orz
謝謝