3
3
import android .app .Activity ;
4
4
import android .app .DownloadManager ;
5
5
import android .content .Intent ;
6
+ import android .content .pm .PackageManager ;
6
7
import android .net .Uri ;
8
+ import android .os .Build ;
9
+ import android .support .v4 .content .FileProvider ;
7
10
import android .util .SparseArray ;
8
11
9
12
import com .facebook .react .bridge .ActivityEventListener ;
24
27
import okhttp3 .OkHttpClient ;
25
28
import okhttp3 .JavaNetCookieJar ;
26
29
30
+ import java .io .File ;
27
31
import java .util .HashMap ;
28
32
import java .util .Map ;
29
33
import java .util .concurrent .LinkedBlockingQueue ;
@@ -105,10 +109,29 @@ public void run() {
105
109
@ ReactMethod
106
110
public void actionViewIntent (String path , String mime , final Promise promise ) {
107
111
try {
108
- Intent intent = new Intent (Intent .ACTION_VIEW )
109
- .setDataAndType (Uri .parse ("file://" + path ), mime );
110
- intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
111
- this .getReactApplicationContext ().startActivity (intent );
112
+ Uri uriForFile = FileProvider .getUriForFile (getCurrentActivity (),
113
+ this .getReactApplicationContext ().getPackageName () + ".provider" , new File (path ));
114
+
115
+ if (Build .VERSION .SDK_INT >= 24 ) {
116
+ // Create the intent with data and type
117
+ Intent intent = new Intent (Intent .ACTION_VIEW )
118
+ .setDataAndType (uriForFile , mime );
119
+
120
+ // Set flag to give temporary permission to external app to use FileProvider
121
+ intent .setFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
122
+
123
+ // Validate that the device can open the file
124
+ PackageManager pm = getCurrentActivity ().getPackageManager ();
125
+ if (intent .resolveActivity (pm ) != null ) {
126
+ this .getReactApplicationContext ().startActivity (intent );
127
+ }
128
+
129
+ } else {
130
+ Intent intent = new Intent (Intent .ACTION_VIEW )
131
+ .setDataAndType (Uri .parse ("file://" + path ), mime ).setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
132
+
133
+ this .getReactApplicationContext ().startActivity (intent );
134
+ }
112
135
ActionViewVisible = true ;
113
136
114
137
final LifecycleEventListener listener = new LifecycleEventListener () {
@@ -382,4 +405,4 @@ public void getSDCardDir(Promise promise) {
382
405
public void getSDCardApplicationDir (Promise promise ) {
383
406
RNFetchBlobFS .getSDCardApplicationDir (this .getReactApplicationContext (), promise );
384
407
}
385
- }
408
+ }
0 commit comments