diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 7e9ac9256..a6a64ce25 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 18 12:33:41 CST 2016 +#Wed Nov 29 11:45:41 CET 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index f99bdc6b0..5b996b1e5 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -30,6 +30,16 @@ android:name=".RNFetchBlobService" > + + + + diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java index cb3c4f269..cd77e037e 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java @@ -3,7 +3,10 @@ import android.app.Activity; import android.app.DownloadManager; import android.content.Intent; +import android.content.pm.PackageManager; import android.net.Uri; +import android.os.Build; +import android.support.v4.content.FileProvider; import com.RNFetchBlob.Utils.DataConverter; import com.facebook.react.bridge.ActivityEventListener; @@ -20,6 +23,7 @@ import com.facebook.react.modules.network.ForwardingCookieHandler; import com.facebook.react.modules.network.OkHttpClientProvider; +import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.concurrent.LinkedBlockingQueue; @@ -101,16 +105,36 @@ public void run() { @ReactMethod public void actionViewIntent(String path, String mime, final Promise promise) { try { - Intent intent= new Intent(Intent.ACTION_VIEW) - .setDataAndType(Uri.parse("file://" + path), mime); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - this.getReactApplicationContext().startActivity(intent); + 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); + + // 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); + + this.getReactApplicationContext().startActivity(intent); + } + ActionViewVisible = true; final LifecycleEventListener listener = new LifecycleEventListener() { @Override public void onHostResume() { - if(ActionViewVisible) + if (ActionViewVisible) promise.resolve(null); RCTContext.removeLifecycleEventListener(this); } @@ -126,7 +150,7 @@ public void onHostDestroy() { } }; RCTContext.addLifecycleEventListener(listener); - } catch(Exception ex) { + } catch (Exception ex) { promise.reject(ex.getLocalizedMessage()); } } diff --git a/android/src/main/res/xml/provider_paths.xml b/android/src/main/res/xml/provider_paths.xml new file mode 100644 index 000000000..7443816be --- /dev/null +++ b/android/src/main/res/xml/provider_paths.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file