Sunil
Sunil

Reputation: 521

different URI scheme across different android version when trying to open a file

I am trying to use my app to open a file from external storage. I am using Share via feature.

my code Export

File filelocation = new File(sPath);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/email");

emailIntent.putExtra(Intent.EXTRA_STREAM, path);
String mystring = ctx.getResources().getString(R.string.Import_rule);
emailIntent.putExtra(Intent.EXTRA_TEXT, mystring);

Import function

if (Intent.ACTION_SEND.equals(in.getAction()))
{
    uri = (Uri) in.getParcelableExtra(Intent.EXTRA_STREAM);

   uri.getPath();

}

When i try to get the uri.getPath() i see difference in differnt devices versions.

in Android 5.0 devicve :

file:///storage/emulated/0/Download/DeviceList.zip

in Android 6.0 device

content://0@media/external/file/1147

Upvotes: 0

Views: 318

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007276

I dont know why the URI scheme is different across versions?

content has been a valid scheme since Android 1.0. It is has been preferred for years due to better security. It will become extremely important in the future, as Android N is beginning to ban file Uri values.

Also, please note that change in schemes this has little to do with the Android OS version. It has more to do with the version of the app you are using to trigger the ACTION_SEND Intent.

How can i resolve this?

Support both of them.

Can you tell me how can i read from content and save as file

Use ContentResolver and openInputStream() to get an InputStream on the content pointed to by the Uri. Then, use Java I/O to copy that data to a local file.

Upvotes: 1

Related Questions