0

I'm trying to open a specific xlsx file in a specific location via some intent but I don't know how to do it.
I have tried to do something like that:

private void DrawNotfication() {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent intent=new Intent(Intent.ACTION_OPEN_DOCUMENT,Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), "file.xlsx")));
    PendingIntent ped= PendingIntent.getActivity(this,0,intent,0);


    Notification notification= new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("file download comlete")
            .setContentIntent(ped).setWhen(System.currentTimeMillis())
            .build();
    mNotificationManager.notify(0,notification);

}

Someone can help me with the syntax?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

I mean to open it in the Excel application

Presumably, Excel supports ACTION_VIEW for this. Replace ACTION_OPEN_DOCUMENT with ACTION_VIEW in your Intent constructor. ACTION_OPEN_DOCUMENT is for displaying what amounts to the file-open dialog you may be used to from other platforms.

Also note that Uri.fromFile() will not work on Android 7.0+, if your targetSdkVersion is 24 or higher. You will need to use FileProvider or something similar, so you can have a Uri with a content scheme instead of a file scheme.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I didn't understand what you said about the uri.. Actually I don't know how intent work so I don't know what is this uri.. can you write for me the specific syntex that I need? – moshe sasson Jan 13 '18 at 18:50
  • @moshe.sasson See [this](https://developer.android.com/about/versions/nougat/android-7.0-changes.html#sharing-files) and [this](https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html) and [this](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) for more about the Android 7.0+ ban on the `file` scheme. – CommonsWare Jan 13 '18 at 18:53
  • I am designing my project for lower versions of android.. Do you know what the syntax I need to write to do this Intent for versions like jellybean? – moshe sasson Jan 20 '18 at 16:38
  • @moshesasson: `ACTION_VIEW` has been around since API Level 1. – CommonsWare Jan 20 '18 at 16:39
  • `Intent intent=new Intent(Intent.ACTION_VIEW,Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS), "file.xlsx")));` this is what I need to write? – moshe sasson Jan 20 '18 at 16:46
  • @moshesasson: If that is where the file is, then yes. – CommonsWare Jan 20 '18 at 17:00
  • I did what you wrote but its open as PDF insted as excel file in the excel app.. How can I fix it? – moshe sasson Jan 29 '18 at 20:28
  • @moshesasson: Contact Microsoft for support on integrating with their Excel app, I guess. – CommonsWare Jan 29 '18 at 20:35