mirror of
https://gitee.com/shuto/cordova-plugin-file-opener2.git
synced 2025-03-15 06:11:00 +08:00
Merge pull request #89 from lincolnthree/master
Fixes https://github.com/pwlin/cordova-plugin-file-opener2/issues/84
This commit is contained in:
commit
74432a4352
13
plugin.xml
13
plugin.xml
@ -16,6 +16,7 @@
|
||||
<!-- Android -->
|
||||
<platform name="android">
|
||||
<source-file src="src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java" target-dir="src/io/github/pwlin/cordova/plugins/fileopener2" />
|
||||
<source-file src="src/android/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java" target-dir="src/io/github/pwlin/cordova/plugins/fileopener2" />
|
||||
<config-file target="res/xml/config.xml" parent="/*">
|
||||
<feature name="FileOpener2">
|
||||
<param name="android-package" value="io.github.pwlin.cordova.plugins.fileopener2.FileOpener2" />
|
||||
@ -24,6 +25,18 @@
|
||||
<config-file target="AndroidManifest.xml" parent="/*">
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
</config-file>
|
||||
<config-file target="AndroidManifest.xml" parent="/manifest/application">
|
||||
<provider
|
||||
android:name="android.support.v4.content.FileProvider"
|
||||
android:authorities="$PACKAGE_NAME.opener.provider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/opener_paths"/>
|
||||
</provider>
|
||||
</config-file>
|
||||
<source-file src="src/android/res/xml/opener_paths.xml" target-dir="res/xml"/>
|
||||
</platform>
|
||||
|
||||
<!-- iOS -->
|
||||
|
@ -23,15 +23,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
package io.github.pwlin.cordova.plugins.fileopener2;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.net.Uri;
|
||||
//import android.util.Log;
|
||||
import android.os.Build;
|
||||
|
||||
import io.github.pwlin.cordova.plugins.fileopener2.FileProvider;
|
||||
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
@ -93,8 +98,25 @@ public class FileOpener2 extends CordovaPlugin {
|
||||
try {
|
||||
Uri path = Uri.fromFile(file);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(path, contentType);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
if(Build.VERSION.SDK_INT >= 24){
|
||||
|
||||
Context context = cordova.getActivity().getApplicationContext();
|
||||
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file);
|
||||
intent.setDataAndType(path, contentType);
|
||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
||||
List<ResolveInfo> infoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
for (ResolveInfo resolveInfo : infoList) {
|
||||
String packageName = resolveInfo.activityInfo.packageName;
|
||||
context.grantUriPermission(packageName, path, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
}
|
||||
}
|
||||
else {
|
||||
intent.setDataAndType(path, contentType);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
}
|
||||
/*
|
||||
* @see
|
||||
* http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
|
||||
|
@ -0,0 +1,7 @@
|
||||
package io.github.pwlin.cordova.plugins.fileopener2;
|
||||
|
||||
/*
|
||||
* http://stackoverflow.com/questions/40746144/error-with-duplicated-fileprovider-in-manifest-xml-with-cordova/41550634#41550634
|
||||
*/
|
||||
public class FileProvider extends android.support.v4.content.FileProvider {
|
||||
}
|
4
src/android/res/xml/opener_paths.xml
Normal file
4
src/android/res/xml/opener_paths.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<external-path name="external_files" path="." />
|
||||
</paths>
|
Loading…
x
Reference in New Issue
Block a user