user983447
user983447

Reputation: 1727

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?

Upvotes: 1

Views: 917

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

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:

  • on Android 4.2+ tablets and Android 5.0+ phones, for guest and other secondary accounts

  • on Android M devices, if the user "adopts" removable storage and moves apps to it

Upvotes: 2

Related Questions