如何查找刚使用相机拍摄的位图文件的名称

时间:2011-12-07 09:43:07

标签: android

我写了一些拍照的代码,我想找到刚刚拍摄的位图文件的名称。

我知道如何获取位图 - 但我不想得到位图 - 我只需要保存在SD卡上的文件名。

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{   
    if (requestCode == 0) 
    {    
        // This is the file name that i need to find ... 
        Bitmap image = (Bitmap) data.getExtras().get("data");  

2 个答案:

答案 0 :(得分:1)

你可以这样做:

//define the file-name to save photo taken by Camera activity
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
mImageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent photoFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoFromCamera.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(photoFromCamera, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

然后当你得到结果时:

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
        if (resultCode == RESULT_OK) {
            Log.i(TAG, "Camera: "+ mImageUri);
            String path = getRealPathFromURI(mImageUri);
            Log.d(TAG, "Result image path from camera: "+path);

        }
        break;

辅助方法:

private String getRealPathFromURI(Uri contentUri) {

    // can post image
    String [] proj={MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery( contentUri,
                    proj, // Which columns to return
                    null,       // WHERE clause; which rows to return (all rows)
                    null,       // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    String res = cursor.getString(column_index);
    cursor.close();

    return res;
}

答案 1 :(得分:0)

您可以在Media上使用FileObserver或存储新照片的目录。 我希望这有帮助!