mirror of
https://gitee.com/shuto/cordova-plugin-file-opener2.git
synced 2025-01-31 12:02:49 +08:00
commit
681d39b140
15
README.md
15
README.md
@ -74,11 +74,24 @@ Notes
|
|||||||
|
|
||||||
- For properly opening _any_ file, you must already have a suitable reader for that particular file type installed on your device. Otherwise this will not work.
|
- For properly opening _any_ file, you must already have a suitable reader for that particular file type installed on your device. Otherwise this will not work.
|
||||||
|
|
||||||
|
|
||||||
- [It is reported](https://github.com/pwlin/cordova-plugin-file-opener2/issues/2#issuecomment-41295793) that in iOS, you might need to remove `<preference name="iosPersistentFileLocation" value="Library" />` from your `config.xml`
|
- [It is reported](https://github.com/pwlin/cordova-plugin-file-opener2/issues/2#issuecomment-41295793) that in iOS, you might need to remove `<preference name="iosPersistentFileLocation" value="Library" />` from your `config.xml`
|
||||||
|
|
||||||
- If you are wondering what MIME-type should you pass as the second argument to `open` function, [here is a list of all known MIME-types](http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co)
|
- If you are wondering what MIME-type should you pass as the second argument to `open` function, [here is a list of all known MIME-types](http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co)
|
||||||
|
|
||||||
|
Android APK installation limitation
|
||||||
|
---
|
||||||
|
The following limitations apply when opening an APK file for installation:
|
||||||
|
- On Android 8+, your application must have the `ACTION_INSTALL_PACKAGE` permission. You can add it by adding this to your app's `config.xml` file:
|
||||||
|
```
|
||||||
|
<platform name="android">
|
||||||
|
<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||||
|
</config-file>
|
||||||
|
</platform>
|
||||||
|
```
|
||||||
|
|
||||||
|
- Before Android 7, you can only install APKs from the "external" partition. For example, you can install from `cordova.file.externalDataDirectory`, but **not** from `cordova.file.dataDirectory`. Android 7+ does not have this limitation.
|
||||||
|
|
||||||
Additional Android Functions
|
Additional Android Functions
|
||||||
---
|
---
|
||||||
The following functions are available in Android platform:
|
The following functions are available in Android platform:
|
||||||
|
@ -102,27 +102,29 @@ public class FileOpener2 extends CordovaPlugin {
|
|||||||
File file = new File(fileName);
|
File file = new File(fileName);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
try {
|
try {
|
||||||
Uri path = Uri.fromFile(file);
|
Intent intent;
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
if (contentType.equals("application/vnd.android.package-archive")) {
|
||||||
if((Build.VERSION.SDK_INT >= 23 && !contentType.equals("application/vnd.android.package-archive")) || ((Build.VERSION.SDK_INT == 24 || Build.VERSION.SDK_INT == 25) && contentType.equals("application/vnd.android.package-archive"))) {
|
// https://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider/9672282#9672282
|
||||||
|
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
||||||
Context context = cordova.getActivity().getApplicationContext();
|
Uri path;
|
||||||
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file);
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||||
|
path = Uri.fromFile(file);
|
||||||
|
} else {
|
||||||
|
Context context = cordova.getActivity().getApplicationContext();
|
||||||
|
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file);
|
||||||
|
}
|
||||||
intent.setDataAndType(path, contentType);
|
intent.setDataAndType(path, contentType);
|
||||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
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);
|
} else {
|
||||||
for (ResolveInfo resolveInfo : infoList) {
|
intent = new Intent(Intent.ACTION_VIEW);
|
||||||
String packageName = resolveInfo.activityInfo.packageName;
|
Context context = cordova.getActivity().getApplicationContext();
|
||||||
context.grantUriPermission(packageName, path, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file);
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
intent.setDataAndType(path, contentType);
|
intent.setDataAndType(path, contentType);
|
||||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @see
|
* @see
|
||||||
* http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
|
* http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- https://developer.android.com/reference/android/support/v4/content/FileProvider.html#SpecifyFiles -->
|
||||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<!-- cordova.file.dataDirectory -->
|
||||||
<files-path name="files" path="." />
|
<files-path name="files" path="." />
|
||||||
<external-files-path name="external_files" path="." />
|
<!-- cordova.file.cacheDirectory -->
|
||||||
<external-path name="external_files" path="." />
|
<cache-path name="cache" path="." />
|
||||||
<cache-path name="cached_files" path="." />
|
<!-- cordova.file.externalDataDirectory -->
|
||||||
<external-cache-path name="cached_files" path="." />
|
<external-files-path name="external-files" path="." />
|
||||||
|
<!-- cordova.file.externalCacheDirectory -->
|
||||||
|
<external-cache-path name="external-cache" path="." />
|
||||||
|
<!-- cordova.file.externalRootDirectory -->
|
||||||
|
<external-path name="external" path="." />
|
||||||
</paths>
|
</paths>
|
||||||
|
Loading…
Reference in New Issue
Block a user