how to get image name using camera intent in andro

2019-05-09 19:21发布

问题:

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

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


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

                    }
            }


回答3:

Try this code,

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

}