1

I tried below code it is working upto Marshmallow it's not working in Nougat..can any one help me how to solve this...

I have a Excelfile in my internal storage...i what open excelfile when button clicked if wps is not install i am redirected to Playstore to Install wps...

I am getting error like:

android.os.FileUriExposedException: file:///storage/emulated/0/Test%20Distributor%20Report.xlsx exposed beyond app through Intent.getData()

void openExcel()
{

    Intent intent = new Intent(Intent.ACTION_VIEW);
    /*vnd.ms-excel*/  /*application/vnd.openxmlformats-officedocument.spreadsheetml.sheet*/
    intent.setDataAndType(Uri.fromFile(file),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

    try {
        context.startActivity(intent);
    }catch (Exception e)
    {
        Toast.makeText(context,"Please Install WPS  Application To View Reports",Toast.LENGTH_LONG).show();

        Intent viewIntent =
                new Intent("android.intent.action.VIEW",
                        Uri.parse("https://play.google.com/store/apps/details?id=cn.wps.moffice_eng&hl=en"));
        context.startActivity(viewIntent);

        Log.d("printexeption",""+e.toString());

    }

}
demo
  • 672
  • 3
  • 9
  • 34

1 Answers1

1

If your targetSdkVersion is 24 or higher, we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps. We create our own class inheriting FileProvider in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies as described here.

Steps to replace file:// uri with content:// uri:

  • add a class extending FileProvider

    public class GenericFileProvider extends FileProvider {
    
    }
    
  • add a FileProvider tag in AndroidManifest.xml under tag. Specify an unique authority for the android:authorities attribute to avoid conflicts, imported dependencies might specify ${applicationId}.provider and other commonly used authorities.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name=".GenericFileProvider"
            android:authorities="${applicationId}.my.package.name.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>
  • then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
  • The final step is to change the line of code below in

    Use the below mentioned code to open excel file :

    File file = new File(Environment.getExternalStorageDirectory()+ "/filepath/" + filename);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/vnd.ms-excel");
    startActivity(intent);
    
  • Edit: If using an intent to make the system open your file, you may need to add the following line of code:

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

Hope this will help...:)

Please refer, full code and solution has been explained here.

If your targetSdkVersion is 24 or higher, you can not use file: Uri values in Intents on Android 7.0+ devices.

Your choices are:

  1. Drop your targetSdkVersion to 23 or lower, or

  2. Put your content on internal storage, then use FileProvider to make it available selectively to other apps

For example:

Intent i=new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));

i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
Nawrez
  • 3,314
  • 8
  • 28
  • 42
  • This code for taking photo...i want to open my excel file from my application – demo Nov 08 '17 at 11:03
  • means you trying to say `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` **instance of use this** `application/vnd.ms-exce` – demo Nov 08 '17 at 11:13
  • 1
    Thank you for you suggestion..i will try sir any problem i come back... – demo Nov 08 '17 at 11:17
  • you can check this to see the difference https://stackoverflow.com/a/4212908/4345934 everyone has a specific mime type – Nawrez Nov 08 '17 at 11:18
  • 1
    i tried all the mime type sir...it's not working...upto Android 6.0 it was working fine all mime types...from Android version 7 it's not working... – demo Nov 08 '17 at 11:20
  • yess tha's true ,as I said in last edit If your targetSdkVersion is 24 or higher, you can not use file: Uri values in Intents on Android 7.0+ devices. – Nawrez Nov 08 '17 at 11:26