如何在Android中使用相机意图获取图像名称?

时间:2011-09-02 10:53:48

标签: android android-camera android-camera-intent

我想使用这个代码来显示相机的当前拍摄图像。我可以从相机获取图像并在imageview中显示。我想知道该图像的文件名。

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;
  }
  }
  }

如何获取图片名称?请帮助朋友,

谢谢朋友

3 个答案:

答案 0 :(得分: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 :(得分: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);
                            }

                    }
            }

答案 2 :(得分: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);
        }
    }
}

}