1

Before Android N in my app after downloading a file I open it like:

Intent myIntent = new Intent(Intent.ACTION_VIEW);
File myFile = new File(result);
String mime = URLConnection.guessContentTypeFromStream(new FileInputStream(myFile));
myIntent.setDataAndType(Uri.fromFile(myFile), mime);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);

Since Android N if I use this code I get an FileUriExposedException, as suggested here I should use the FileProvider and get the path like:

Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

and then open it with:

ParcelFileDescriptor.openFile(Uri uri,String mode)

as suggested here where it says Uri: A content URI associated with a file, as returned by getUriForFile().

But in the IDE if I do ParcelfileDescriptor.open... there is no method openFile() Image

only open() with a File as parameter, and no uri. So how I can open a file in Android N?

Note: I don't want to open a file with my application. For example if I download a pdf, I want to open it with the installed app on the phone.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Simone Luconi
  • 86
  • 1
  • 1
  • 9

1 Answers1

1

Since Android N if I use this code I get an FileUriExposedException, as suggested here I should use the FileProvider

That part is correct. The Uri that you get from getUriForFile() then goes in your ACTION_VIEW Intent. So you wind up with something like:

File myFile = new File(result);
Uri uri = FileProvider.getUriForFile(context, YOUR_AUTHORITY, myFile);
Intent myIntent = new Intent(Intent.ACTION_VIEW, uri);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I use this code but its shows error can't play video.... Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(intentUri, "video/*"); startActivity(intent); – Bhavin Patel Jan 10 '17 at 12:47
  • @bdevloper: Talk to the developers of the video player app. – CommonsWare Jan 10 '17 at 13:12
  • @bdevloper: So? If the video player app is crashing, at minimum, there are bugs in the video player app. While there may be ways that you can address the crash, since we do not have your app and we do not have the video player app, it will be difficult for anyone here to help you. – CommonsWare Jan 10 '17 at 13:28