2013-02-21 132 views
0

我試圖讓我用相機拍攝的照片作爲字符串的路徑。當我嘗試用我的代碼拍攝照片時,字符串爲空。當我關閉我的應用程序時,打開相機應用程序並在那裏拍照,然後重新打開我的應用程序並使用我的代碼,然後獲取我使用相機應用程序拍攝的最後一張照片的字符串。我的問題是爲什麼我的相機意圖不工作,爲什麼當我使用相機應用程序工作?相機意圖圖片文件路徑

這裏是我的代碼:

public void onClick(View arg0) { 
    switch (arg0.getId()) {  
     case R.id.btnImageCapture: 
     Intent openCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(openCamera, OPEN_CAMERA); 
        break; 
    } 
} 

private String getLastImagePath() { 
      final String[] imageColumns = { MediaStore.Images.Media._ID, 
        MediaStore.Images.Media.DATA }; 
      final String imageOrderBy = MediaStore.Images.Media._ID + " DESC"; 
      Cursor imageCursor = managedQuery(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, 
        null, null, imageOrderBy); 
      if (imageCursor.moveToFirst()) { 
       String fullPath = imageCursor.getString(imageCursor 
         .getColumnIndex(MediaStore.Images.Media.DATA)); 
       imageCursor.close(); 
       return fullPath; 
      } 
     } 

 @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch(requestCode){ 
     case OPEN_CAMERA: 
      if (resultCode == RESULT_OK && data != null) { 
       String picturePath = getLastImagePath(); 
       break; 
      } 
      } 
     } 
+0

你爲什麼不使用給了你在onActivityResult圖像的URI? data.getData(); – dymmeh 2013-02-21 18:28:37

+0

這是因爲我需要文件路徑。 ;) – 2013-02-21 18:31:48

+0

用Uri試試我的答案。 – dymmeh 2013-02-21 18:57:05

回答

2

試試這個。它的工作原理..

private Uri imageUri; 

    public void onClick(View arg0) { 
    switch (arg0.getId()) {  
     case R.id.btnImageCapture: 

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      Uri.fromFile(photo)); 
    imageUri = Uri.fromFile(photo); 
    startActivityForResult(intent, OPEN_CAMERA); 
        break; 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case OPEN_CAMERA: 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImage = imageUri; 
      getContentResolver().notifyChange(selectedImage, null); 
      ImageView imageView = (ImageView) findViewById(R.id.ImageView); 
      ContentResolver cr = getContentResolver(); 
      Bitmap bitmap; 
      try { 
       bitmap = android.provider.MediaStore.Images.Media 
       .getBitmap(cr, selectedImage); 

       imageView.setImageBitmap(bitmap); 
       Toast.makeText(this, selectedImage.toString(), 
         Toast.LENGTH_LONG).show(); 

      } catch (Exception e) { 
       Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
         .show(); 

      } 
     } 
    } 
} 
+0

我肯定需要文件路徑,因爲我必須將它保存在SQLite數據庫中。 – 2013-02-21 18:35:22

+0

是的,你可以從我的答案得到.. – moDev 2013-02-21 18:36:16

+0

它的工作原理? – moDev 2013-02-21 19:17:43