Skip to content

Commit

Permalink
Merge pull request #685 from Chris-Guy/master
Browse files Browse the repository at this point in the history
[Android] Fix actionViewIntent for SDK < 24
  • Loading branch information
rpenfold authored Apr 4, 2022
2 parents 4a2f8c4 + 029cf17 commit 28f4a54
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,23 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(),
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));

if (Build.VERSION.SDK_INT >= 24) {
// Create the intent with data and type
Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(uriForFile, mime);

// 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 intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Create the intent with data and type
Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(uriForFile, mime);

// 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 {
promise.reject("EUNSPECIFIED", "Cannot open the URL.");
}

ActionViewVisible = true;

final LifecycleEventListener listener = new LifecycleEventListener() {
Expand Down

0 comments on commit 28f4a54

Please sign in to comment.