2

Dear stack overflow, The concept of an URI on Android is wonderful, and it abstracts out how we point to different things on the device and internet, but it does need careful thought to work with them. I have couple of questions for you.

1) How do people generally consume remote URI for uploading into their own server ? The best I can think of is, downloading the remote URI to disk, and giving a pointer to the file to okhttp. Is this the best way to go about doing it ?

2) Second is a more basic question with respect to local URI's. URI's can point to different things, and the absolute path to the file is stored at various places for various kinds of providers. Please see #getPath in this link .

Since okhttp works only with absolute files, is this logic present in that file the best way to do about sending the file via okhttp.

Or do you suggest anything better ?

Let us assume that for simplicity sake, we are developing for Kitkat and above.

1 Answers1

2

1) yes. the best way is to guarantee you have access it until the completion of your upload (specially for example if you have a resume feature, where the upload could be interrupted and later on re-started), is to have a copy of it local to your own application. URI can disappear or not be available later on.

2) yes. see 1) use files, they are concrete and reliable.

edit:

as per your comments I realised that my answer was not very well explained. What I meant by "use a file" is to use context.getContentResolver().openInputStream(uri) to save a file locally to your own application using context.openFileOutput("temp_file", 0) or context.getFilesDir() and then you can use safely use this file.

All those methods on FileUtils to try to guess a path for the URI are flaky and have edge cases and will only give trouble.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Files are concrete and reliable accepted. But the problem is the URI does not consistently expose the absolute file path. Trying to find the absolute file path is exactly the opposite thinking of why URI's were developed in the first place. I used FileUtils.getPath for a content URI from the Google Drive app, the function returns a null. Lets say I find out what the path is... tomorrow if some other app comes up with a different location for file path.. do we have to keep adding new code to get path ? How about using : – Nitesh Mudireddy Apr 18 '17 at 23:06
  • http://stackoverflow.com/questions/25367888/upload-binary-file-with-okhttp-from-resources – Nitesh Mudireddy Apr 18 '17 at 23:08
  • please see my edit. my answer was not very well explained. – Budius Apr 19 '17 at 18:21
  • Thank you for the clarification. I agree. It looks like currently this is the best way to go about it. But the inefficiency it causes is not something I am a big fan of. Just imagine a 100MB file, that needs to be copied , just for a file upload. Will Accept your answer . – Nitesh Mudireddy Apr 20 '17 at 15:41
  • It would be interesting if Retrofit could take a InputStream instead, but without this option... Nothing we can do – Budius Apr 21 '17 at 13:09