1

This is the code i used to get the uri of my image Uri imageUri = data.getData();

How do I get the actual path of an image selected?

the Uri value/path that i am currently getting is

content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FLightStick%2F144pixels.bmp

the correct filepath of the image that i need is for my other function is

/storage/emulated/0/LightStick/144pixels.bmp

The image selection function:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT_OK){//everything processed successfully
        if(requestCode == IMAGE_GALLERY_REQUEST){ //hearing back from image gallery

            //the address of the image on the SD card
            Uri imageUri = data.getData();
            BMPfilepath =imageUri.getPath();
            //stream to read image data from SD card
            InputStream inputStream;

            try {
                inputStream = getContentResolver().openInputStream(imageUri);//getting an input stream, based no the URI of image
                Bitmap image = BitmapFactory.decodeStream(inputStream);//get bitmap from stream
                imgPicture.setImageBitmap(image);//show image to user in imageview

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show(); //let user know image unavail
            }//catch
        } //requestCode == IMAGE_GALLERY_REQUEST
    }

The upload function which uses the imageUri from the previous function. String path = imageUri.getPath().toString(); the app crashes and goes to a looper file when in debug

public void onUploadToArduino(){
    String path = imageUri.getPath().toString();//<- app crashes and goes to a looper file when in debug
    String sdpath = System.getenv("EXTERNAL_STORAGE");
    String extStore = Environment.getExternalStorageDirectory().getPath();
    String FILENAME = extStore + "/LightStick/144pixels.bmp";

    String collected = null;
    FileInputStream fis = null;

    FileInputStream fileInputStream = null;
    byte[] bytesArray = null;

    try {
        File file = new File(FILENAME);//<- this works
        //File file = new File(path);//<- this doesnt
        bytesArray = new byte[(int) file.length()];

        //read file into bytes[]
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bytesArray);
tzj
  • 93
  • 8
  • Have you checked [this](https://stackoverflow.com/a/13209522/2591002) and [this](https://stackoverflow.com/a/8892463/2591002)? – SweetWisher ツ Oct 15 '17 at 12:06
  • 2
    Possible duplicate of [Get filename and path from URI from mediastore](https://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore) – SweetWisher ツ Oct 15 '17 at 12:06
  • @SweetWisherツ: All three of your links focus on flawed solutions to the problem. For example, none of them would handle the `Uri` in the question. – CommonsWare Oct 15 '17 at 12:08
  • 1
    why cannot you use `Uri imageUri` in our `"other function"`? what do you want to do with that `Uri`s data? – pskink Oct 15 '17 at 12:17
  • @pskink , on 1 button, i let the users select the image and display it in an image view. on another button, the upload button, the function uses the path of the selected image, and sends the file byte by byte to my microcontroller – tzj Oct 15 '17 at 13:16
  • then use `ImageView#setImageURI` – pskink Oct 15 '17 at 13:16
  • @pskink , apologies, i pressed enter too soon, before i completed my sentence. – tzj Oct 15 '17 at 13:17
  • so use `ContentResolver#openInputStream() ` – pskink Oct 15 '17 at 13:18
  • @SweetWisherツ , yes, ive seen those. most of the solutions are not working for Android 4.4+. uri.getPath(); also does not work(app just closes). in 1 function, the user selects an image, and is stored in the URI `Uri imageUri = data.getData();` in another function, it tries to get the filepath from the URI by `imageUri.getPath();` and yes, imageUri is global(is in the scope of the entire class) – tzj Oct 15 '17 at 13:21
  • do not use workarounds like those proposed by sweetwisher, use `ContentResolver#openInputStream() ` instead – pskink Oct 15 '17 at 13:23
  • @pskink i am sorry, i am very relatively new to android and OOP programming.. I've edited my question to include necessary parts of my code. where do i put the `ContentResolver#openInputStream()` ? – tzj Oct 15 '17 at 13:30
  • where you use [InputStream](https://developer.android.com/reference/java/io/InputStream.html) – pskink Oct 15 '17 at 13:31

1 Answers1

2

How do I get the actual path of an image selected?

You don't. A Uri is not a file and may not point to a file, let alone one that you can access.

Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Ideally, just use the stream. If you need a File for some other API that is poorly written and does not support streams:

  • Create a FileOutputStream on some File that you control (e.g., in getCacheDir())

  • Use the InputStream and the FileOutputStream to copy the bytes to your file

  • Use your file

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491