Android 7 open file cyrillic name

138 views Asked by At

I have a file with cyrillic name:

File file = new File(cyr_name);

and I try to open it in another application:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file ), "text/plain");
startActivity(intent);

In android version 6 is no problem. Android 7 is error:

28-12 20:54:25.018 21665 21665 E AndroidRuntime: android.os.FileUriExposedException: file:///storage/emulated/0/ws/%D0%9C%D0%B5%D1%82%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%B8%D0%B9_%D0%A3%D0%BD%D0%B5%D1%81%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%B2%D0%B5%D1%82%D1%80%D0%BE%D0%BC%20%D0%9A%D0%BD%D0%B8%D0%B3%D0%B0%20%D1%87%D0%B5%D1%82%D0%B2%D0%B5%D1%80%D1%82%D0%B0%D1%8F.txt exposed beyond app through Intent.getData()

What happened to the 7 android?

1

There are 1 answers

0
Steve M On BEST ANSWER

It has nothing to do with cyrillic, It's because you're using a file scheme intent which can no longer be passed to other apps. You must pass a content intent with a FileProvider or set a security policy to override this behavior. See https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en for example of using a FileProvider.

Here is the way to override the security policy if you go that route. It sets the penalty for exposing a file intent to log instead of process death. You can place it in some onCreate() before you send the intent.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build();
            StrictMode.setVmPolicy(policy);
}