how to get image name using camera intent in android?

10,701

Solution 1

In your activity (called YourActivity):

public static int TAKE_IMAGE = 111;
Uri mCapturedImageURI;

Somewhere call the camera!

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

Now in the Activity result (notice 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);
    }
}

Solution 2

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

                    }
            }
Share:
10,701
John
Author by

John

Updated on July 12, 2022

Comments

  • John
    John almost 2 years

    I want to show camera's current take image using like given this code.I can get image from camera and display in imageview.I want to know that image's file name.

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

    How can i get image name?please help friends,

    Thanks Friends

  • Veeru
    Veeru over 11 years
    That worked beautifully, i was almost giving up after 6 hours, till i found this sherif. Upvote for you. To give more info for somebody having same problem as me, i was working with aviary and i could'nt find a way to pass the image name to aviary intent, this was what helped me. Thanks again :)
  • Rilcon42
    Rilcon42 almost 11 years
    To fix the deprecation problem I believe you can replace: Cursor cursor = managedQuery(mCapturedImageURI, projection, null,null, null); with: Cursor cursor = getContentResolver().query(mCapturedImageURI, projection, null, null, null);