Skip to content

Commit 1c52a84

Browse files
authored
Merge pull request #81 from Aure77/bugfix/open-file-intent-android-11
Fix actionViewIntent open file intent with Android 11+ devices
2 parents 5674573 + b540134 commit 1c52a84

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,29 @@ public void actionViewIntent(String path, String mime, @Nullable String chooserT
118118
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
119119

120120
Intent intent = new Intent(Intent.ACTION_VIEW);
121-
if (Build.VERSION.SDK_INT >= 24) {
121+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
122122
// Create the intent with data and type
123123
intent.setDataAndType(uriForFile, mime);
124-
if (chooserTitle != null) {
125-
intent = Intent.createChooser(intent, chooserTitle);
126-
}
127124

128125
// Set flag to give temporary permission to external app to use FileProvider
129126
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
130127
// All the activity to be opened outside of an activity
131128
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
132-
133-
// Validate that the device can open the file
134-
PackageManager pm = getCurrentActivity().getPackageManager();
135-
if (intent.resolveActivity(pm) != null) {
136-
this.getReactApplicationContext().startActivity(intent);
137-
}
138-
139129
} else {
140130
intent.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
141-
if (chooserTitle != null) {
142-
intent = Intent.createChooser(intent, chooserTitle);
143-
}
131+
}
144132

145-
try {
146-
this.getReactApplicationContext().startActivity(intent);
147-
promise.resolve(true);
148-
} catch (ActivityNotFoundException ex) {
149-
promise.reject("ENOAPP", "No app installed for " + mime);
150-
}
133+
if (chooserTitle != null) {
134+
intent = Intent.createChooser(intent, chooserTitle);
151135
}
136+
137+
try {
138+
this.getReactApplicationContext().startActivity(intent);
139+
promise.resolve(true);
140+
} catch (ActivityNotFoundException ex) {
141+
promise.reject("ENOAPP", "No app installed for " + mime);
142+
}
143+
152144
ActionViewVisible = true;
153145

154146
final LifecycleEventListener listener = new LifecycleEventListener() {

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ export interface AndroidApi {
509509
* @param mime Basically system will open an app according to this MIME type.
510510
* @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)))
511511
*/
512-
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<any>;
512+
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<boolean | null>;
513513

514514
/**
515515
*

0 commit comments

Comments
 (0)