How to get path of a captured image in android

Viewed 23482

I am working on an android application. It has a functionality that captured image should display on screen and I need to get that image path, so that I can send that image to server.

I am able to display image on screen, but unable to get absolute image path.

I am getting path like:

content://media/external/images/media/1220
content://media/external/images/media/1221

How can I get actual image path?

Here is my code:

mintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mintent.putExtra(MediaStore.EXTRA_OUTPUT,
                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
startActivityForResult(mintent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if ((data != null && !data.toString().equalsIgnoreCase("Intent {  }"))
      || requestCode == 1)
    switch (requestCode) {
      case 1:
        try {
          // Uri imageFileUri = data.getData();
          // String path=getRealPathFromURI(this,imageFileUri);
          // Bitmap bitmap = (Bitmap) data.getExtras().get("data");

          String photoPath = null;
          Bitmap bitmap = (Bitmap) data.getExtras().get("data");
          Intent intent = new Intent(this, Media.class);
          intent.putExtra("BitmapImage", bitmap);

          Bundle b = mintent.getExtras();
          if (b != null && b.containsKey(MediaStore.EXTRA_OUTPUT)) { // large
            // image?
            String timeStamp = new SimpleDateFormat(
                "yyyyMMdd_HHmmss").format(new Date());
            // Shouldn't have to do this ... but
            photoPath = MediaStore.Images.Media.insertImage(
                        getContentResolver(), bitmap, "Img" + timeStamp
                        + ".jpeg", null);
          }
        }
    }
}

photoPath value is shown in above.

How can I get my absolute image path from this path.

4 Answers

Try like this,

    void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
            knop.setVisibility(Button.VISIBLE);
    
            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);
    
            // CALL THIS METHOD TO GET THE ACTUAL PATH
            File finalFile = new File(getRealPathFromURI(tempUri));
    
            System.out.println(mImageCaptureUri);
        }  
    }
    
    public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }
    
    public String getRealPathFromURI(Uri uri) {
        String path = "";
        if (getContentResolver() != null) {
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
                int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                path = cursor.getString(idx);
                cursor.close();
            }
        }
        return path;
    }
Related