2014-12-05 93 views
0

我使用下面的代碼打開相機後的圖像的路徑獲取存儲在庫相機捕捉

Intent captureImageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

cordova.setActivityResultCallback(this); 

cordova.getActivity().startActivityForResult(captureImageIntent,RESULT_CAPTURE_IMAGE); 

和內部onActivityResult我試圖獲取存儲在庫中的path of the image,這樣我可以返回它回到網頁。

這裏

我曾嘗試迄今

Uri uri = intent.getData(); // doesnt work 

我試圖用MediaStore.EXTRA_OUTPUT但在這種情況下,我得到空的意圖。

captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri); 

所以任何人都可以告訴我如何獲取路徑?

編輯

String fileName = "temp.jpg"; 
contentValues values = new ContentValues(); 
values.put(MediaStore.Images.Media.TITLE, fileName); 

      Uri mPhotoUri = cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
+0

請問如何獲得mPhotoUri? – 2014-12-05 10:20:59

+0

@HareshChhelana查看編輯 – Hunt 2014-12-05 10:28:17

回答

1

定義組自定義方法,並得到拍攝的圖像路徑:

captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); 

獲取拍攝的圖像bitamp:

private String imgPath; 

public Uri setImageUri() { 
    // Store image in dcim 
    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg"); 
    Uri imgUri = Uri.fromFile(file); 
    imgPath = file.getAbsolutePath(); 
    return imgUri; 
} 

public String getImagePath() { 
    return imgPath; 
} 

設置圖像的URI使用捕捉意圖EXTRA_OUTPUT來自已解碼的捕獲圖像路徑:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == RESULT_CAPTURE_IMAGE) { 
      imgUserImage.setImageBitmap(decodeFile(getImagePath())); 
     } 
    } 
} 

public Bitmap decodeFile(String path) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, o); 
     // The new size we want to scale to 
     final int REQUIRED_SIZE = 70; 

     // Find the correct scale value. It should be the power of 2. 
     int scale = 1; 
     while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
     scale *= 2; 
     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeFile(path, o2); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
return null; 
}