2011-07-11 50 views
2

像標題說,我的問題涉及相機拍攝的圖像uri圖像。 Uri始終爲空......爲什麼?Android - 問題獲取圖像uri

@Override 
public void onClick(View v) { 

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
    startActivityForResult(cameraIntent, TAKE_PICTURE); 


@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 

     if (requestCode == TAKE_PICTURE) 
     { 
      //Uri contentURI = Uri.parse(data.getDataString()); 
      thumbnail = (Bitmap) data.getExtras().get("data"); 
      ImageView image = (ImageView) findViewById(R.id.photoResultView); 
      image.setImageBitmap(thumbnail); 

       photoUri = data.getData(); 
       Log.e("FOTOLOG",""+ photoUri); 


     } 
    } 

回答

5

看看這個,

Android ACTION_IMAGE_CAPTURE Intent

可能會爲你提供一些見解。

public void startCamera() { 

    File photo = null; 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED)) { 
     photo = new File(android.os.Environment 
       .getExternalStorageDirectory(), FILE_NAME); 
    } else { 
     photo = new File(getCacheDir(), FILE_NAME); 
    }  
    if (photo != null) { 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
     selectedImageUri = Uri.fromFile(photo); 
     startActivityForResult(intent, IMAGE_CAPTURE); 
    } 
} 


public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == IMAGE_CAPTURE) { 

     if (resultCode == Activity.RESULT_OK) { 

      try { 
       Uri selectedImage = selectedImageUri; 
       //getContentResolver().notifyChange(selectedImage, null); 
       ContentResolver cr = getContentResolver(); 
       Bitmap bitmap; 
       bitmap = android.provider.MediaStore.Images.Media 
         .getBitmap(cr, selectedImage); 
       rptImage.setImageBitmap(bitmap); 

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

     } else { 
      selectedImageUri = null; 
      rptImage.setImageBitmap(null); 
     } 

    } 

} 

這是我的應用程序的代碼示例。

您需要的權限,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-feature android:name="android.hardware.camera" /> 
+0

我試圖修復該錯誤的描述,但沒有改變... –

+0

@massimo確定我得到了我使用:)希望工程 – Hades

+0

感謝一個代碼示例運作良好 –