2011-09-02 57 views
3

我想用相似的代碼顯示相機的當前拍攝圖像。我可以從相機獲取圖像並在imageview中顯示。我想知道該圖像的文件名。如何在Android中使用相機意圖獲取圖像名稱?

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, CAMERA_PIC_REQUEST); 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
switch (requestCode) 
{ 
case 2: 
{ 
    if (resultCode == RESULT_OK) 
    { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    mg_view.setImageBitmap(thumbnail); 
    } 
    break; 
    } 
    } 
    } 

我怎樣才能像名稱請朋友們有所幫助,

感謝網友

回答

7

在你的活動(稱爲YourActivity):

public static int TAKE_IMAGE = 111; 
Uri mCapturedImageURI; 

某處調用攝像頭!

try { 
    String fileName = "temp.jpg"; 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.Images.Media.TITLE, fileName); 
    mCapturedImageURI = getContentResolver() 
      .insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        values); 
    Intent intent = new Intent(
      MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      mCapturedImageURI); 
    startActivityForResult(intent, TAKE_IMAGE); 
} catch (Exception e) { 
    Log.e("", "", e); 
} 
在活動結果

現在(通知capturedImageFilePath

protected void onActivityResult(int requestCode, int resultCode, 
     Intent intent) { 
    if ((requestCode == YourActivity.TAKE_IMAGE) 
      && (resultCode == RESULT_OK)) { 
     mode = MODE_VIEWER; 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(mCapturedImageURI, projection, null, 
       null, null); 
     int column_index_data = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 

     //THIS IS WHAT YOU WANT! 
     String capturedImageFilePath = cursor.getString(column_index_data); 

     bitmap = BitmapFactory.decodeFile(capturedImageFilePath); 
    } 
} 
+1

,完美的工作,我幾乎放棄6個小時後,直到我找到這個sherif。 Upvote給你。 要給與我有同樣問題的人提供更多信息,我正在與鳥舍合作,並且我無法找到將圖像名稱傳遞給鳥舍意圖的方式,這對我有幫助。 再次感謝:) – Veeru

+0

managedQuery已被棄用? – RRTW

+0

@RRTW是的,我應該更新此代碼 –

3
sample code. May be it is useful to you. 

Uri mUri; 
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "imgnm_"+ String.valueOf(System.currentTimeMillis())+ ".jpg")); 
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mUri); 
intent.putExtra("return-data", true); 
startActivityForResult(intent, PICK_CAMERA_IMAGE); 


public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == PICK_CAMERA_IMAGE) { 
     if (resultCode == RESULT_OK) { 
     String path = mUri.getPath(); 

     if (path.length() > 0) { 
          String filepath = path; 
          String filename = filepath.substring(filepath.lastIndexOf("/") + 1,filepath.length()); 
           String filetype = ".jpg"; 
           Bitmap bm = BitmapFactory.decodeFile(filepath); 
           mg_view.setImageBitmap(bm); 
          } 

        } 
      } 
+0

謝謝我一直在尋找這個。 –

0

試試這個代碼,

Log.d("ANDRO_CAMERA", "Starting camera on the phone..."); 
    String fileName = "testphoto.jpg"; 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.Images.Media.TITLE, fileName); 
    values.put(MediaStore.Images.Media.DESCRIPTION, 
      "Image capture by camera"); 
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
    imageUri = getContentResolver().insert(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
    startActivityForResult(intent, IMAGE_CAPTURE); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == IMAGE_CAPTURE) { 
     if (resultCode == RESULT_OK){ 
      Log.d("ANDRO_CAMERA","Picture taken!!!"); 
      imageView.setImageURI(imageUri); 
     } 
    } 
} 

}