0

i'm following along this answer: Android- how can I convert android.net.Uri object to java.net.URI object?

android.net.Uri INPUT_FILE;
java.net.URI juri = new java.net.URI(INPUT_FILE.toString());
File inputFile = new File(juri);

I get this error; java.lang.IllegalArgumentException: URI scheme is not "file"

The file is an mp4 inside the android Downloads directory. I'm currently using pixel 2 api 29. Any recommendations on how to fix the problem.

Arun
  • 1,599
  • 5
  • 19
  • 33

1 Answers1

1

The problem you have is that a File must be a file on disk. That means the URI must start with file:/// Your URI doesn't. Either your URI is malformed, or it isn't to a local file. Either of those would need to be fixed.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I'm getting that URI from the Android file browser, inside the emulator. I'm using the code above to open the URI in the downloads folder, and then use some sample mp4 code. The URI I see is; content://com.android.providers.downloads.documents/document/44. So it looks like I dont have the file:// in the beginning. I see this thread; https://stackoverflow.com/questions/5657411/android-getting-a-file-uri-from-a-content-uri . Do you have an alternate suggestion? – Arun Nov 09 '21 at 19:49
  • 1
    @Arun That's not a file URI, it's a content URI. It cannot be turned into a FIle object. It ma not even be a file, it can be returned programatically by a content provider, fetched from a db, calculated at runtime, etc. You can't assume its a file, and you can't access it via file commands. The accepted answer there looks good, that will give you an InputStream that should allow you to read the contents. – Gabe Sechan Nov 09 '21 at 19:56