androidandroid-contentproviderfile-uri

how do URIs with "file" scheme work in Android


I know that in Android I can read files from disk this way:

Uri uri = Uri.parse("file:///data/data/com.example.mytest.mytest/test.txt");
InputStream stream = getContentResolver().openInputStream(uri);
int i;
while ((i = stream.read()) != -1) {
  Log.i("@@@", "znak: " + i);
}

However, I don't know how it works. Does exist in Android some content provider that works with "file" scheme? But how - I thought that the scheme of URIs of all content providers must have "content" scheme?


Solution

  • ContentResolver and openInputStream() handle file: schemes directly. This is covered in the documentation for openInputStream().

    Also, do not hardcode paths. Your Uri is wrong on millions of devices: