Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file path of image on Android

Tags:

I have an app that can make pictures and upload them. The upload requires the file path of the photo but I can't get it.

This is my code:

public void maakfoto (View v) {      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);     startActivityForResult(cameraIntent, CAMERA_REQUEST);  }  protected 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);         System.out.println(mImageCaptureUri);     }   } 

Please help me to get the file path.

like image 211
Rick de Jong Avatar asked Mar 15 '13 12:03

Rick de Jong


People also ask

How do I get the file path of a picture?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get a list of images in a specific folder on Android?

You can use below code to get all images from specific folder. 1) First you need to define File object to get the storage and appened the name of the folder you want to read. File folder = new File(Environment. getExternalStorageDirectory().


1 Answers

Posting to Twitter needs the image's actual path on the device to be sent in the request to post. I was finding it mighty difficult to get the actual path and more often than not I would get the wrong path.

To counter that, once you have a Bitmap, I use that to get the URI from using the getImageUri(). Subsequently, I use the tempUri Uri instance to get the actual path as it is on the device.

This is production code and naturally tested. ;-)

protected 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) {     Cursor cursor = getContentResolver().query(uri, null, null, null, null);      cursor.moveToFirst();      int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);      return cursor.getString(idx);  } 
like image 141
Siddharth Lele Avatar answered Sep 28 '22 03:09

Siddharth Lele