1. 程式人生 > >android 呼叫系統相機並獲取圖片路徑

android 呼叫系統相機並獲取圖片路徑

思路: 呼叫系統的相機之前,設法指定圖片的路徑,
然後從指定的路徑中獲取圖片.不要從返回的意圖Intent中獲取圖片的路徑,
因為,不同的手機廠商對此的處理不同.有些可能會獲取到圖片的路徑,有些卻返回null.

    /**
     * 呼叫系統拍照
     */
    public void photo(){

        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        ContentValues values = new ContentValues(); 
        photoUri=
        this
.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, TAKE_PICTURE); }

在onActivityResult 中處理:

/**
     * 從系統拍照獲取圖片路徑
     * @return
     */
    private
String pathFromPhoto() { String picPath=null; String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(photoUri, pojo, null, null,null); if(cursor != null ) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); try
{ if(Integer.parseInt(Build.VERSION.SDK) <14){ cursor.close(); } } catch (NumberFormatException e) { Log.v("qin","error:"+e); } } return picPath; }