0

I am trying to get the actual path of an image so that I can upload or send to server as multipart/form-data. So, Once I get the Uri, I have below code

Uri selectedImageUri = data.getData();
                File file = new File(selectedImageUri.getPath());//create path from uri
                final String[] split = file.getPath().split(":");//split the path.
                String path = split[1];//assign it to a string(your choice).

   
uri value = content://com.android.providers.media.documents/document/image%3A32 
path value = 32

I don't get it why path is a number, even I tried using getcontentResolver() and with that too I am getting path =32. I am using API 29 so I can't use MediaStore.Images.Media.DATA so, I have to use MediaStore.Images.Media._ID. It would be good if someone can also tell how to upload image as form-data too once i have path.

Mr. Jay
  • 153
  • 2
  • 5
  • 16
  • You do not necessarily have filesystem access to the images, and you do not need a filesystem path to upload it. Just use the `Uri`. See [this](https://stackoverflow.com/a/56308643/115145) and [this](https://commonsware.com/blog/2020/07/05/multipart-upload-okttp-uri.html). – CommonsWare Nov 21 '20 at 23:59
  • thanks. I am trying to send uri as inputstream to sever but looks like there is something wrong with the bytes as from server i am getting Form key or value length limit 2048 exceeded though if i upload same image from postman to sever it works fine. OutputStream outputStream = conn.getOutputStream(); byte[] buffer = new byte[maxBufferSize]; bytesRead = -1; InputStream inputStream = getContentResolver().openInputStream(uri); while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); – Mr. Jay Nov 22 '20 at 18:38

0 Answers0