Skip to content

Commit

Permalink
Merge pull request #81 from Aure77/bugfix/open-file-intent-android-11
Browse files Browse the repository at this point in the history
Fix actionViewIntent open file intent with Android 11+ devices
  • Loading branch information
RonRadtke authored Nov 15, 2021
2 parents 5674573 + b540134 commit 1c52a84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,29 @@ public void actionViewIntent(String path, String mime, @Nullable String chooserT
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));

Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Create the intent with data and type
intent.setDataAndType(uriForFile, mime);
if (chooserTitle != null) {
intent = Intent.createChooser(intent, chooserTitle);
}

// Set flag to give temporary permission to external app to use FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// All the activity to be opened outside of an activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Validate that the device can open the file
PackageManager pm = getCurrentActivity().getPackageManager();
if (intent.resolveActivity(pm) != null) {
this.getReactApplicationContext().startActivity(intent);
}

} else {
intent.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (chooserTitle != null) {
intent = Intent.createChooser(intent, chooserTitle);
}
}

try {
this.getReactApplicationContext().startActivity(intent);
promise.resolve(true);
} catch (ActivityNotFoundException ex) {
promise.reject("ENOAPP", "No app installed for " + mime);
}
if (chooserTitle != null) {
intent = Intent.createChooser(intent, chooserTitle);
}

try {
this.getReactApplicationContext().startActivity(intent);
promise.resolve(true);
} catch (ActivityNotFoundException ex) {
promise.reject("ENOAPP", "No app installed for " + mime);
}

ActionViewVisible = true;

final LifecycleEventListener listener = new LifecycleEventListener() {
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export interface AndroidApi {
* @param mime Basically system will open an app according to this MIME type.
* @param chooserTitle title for chooser, if not set the chooser won't be displayed (see [Android docs](https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence)))
*/
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<any>;
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<boolean | null>;

/**
*
Expand Down

0 comments on commit 1c52a84

Please sign in to comment.