Skip to content

Commit

Permalink
fix openFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mcagabe19 committed Dec 10, 2024
1 parent 079fd04 commit 42adf86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
9 changes: 9 additions & 0 deletions templates/android/template/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ android {
}
}

::if (ANDROID_USE_ANDROIDX)::
buildFeatures {
buildConfig = true
}
::end::

android.applicationVariants.all { variant ->
variant.outputs.all { output ->
if (outputFileName != null && outputFileName.endsWith('.apk')) {
Expand All @@ -91,6 +97,9 @@ android {
}

dependencies {
::if (ANDROID_USE_ANDROIDX)::
implementation 'androidx.core:core:1.6.0'
::end::
implementation fileTree(dir: 'libs', include: ['*.jar'])
::if (ANDROID_LIBRARY_PROJECTS)::::foreach (ANDROID_LIBRARY_PROJECTS)::implementation project(':deps:::name::')
::end::::end::
Expand Down
11 changes: 11 additions & 0 deletions templates/android/template/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@

</activity>

::if (ANDROID_USE_ANDROIDX)::
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="::APP_PACKAGE::.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
::end::

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
::if (ANDROID_USE_ANDROIDX)::
import androidx.core.content.FileProvider;
import ::APP_PACKAGE::.BuildConfig;
::end::
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -344,9 +348,22 @@ public static void openFile (String path) {
String mimeType = MimeTypeMap.getSingleton ().getMimeTypeFromExtension (extension);
File file = new File (path);

Intent intent = new Intent ();
intent.setAction (Intent.ACTION_VIEW);
intent.setDataAndType (Uri.fromFile (file), mimeType);
Uri uri;
::if (ANDROID_USE_ANDROIDX)::
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Android 7.0+
uri = FileProvider.getUriForFile(Extension.mainActivity, BuildConfig.APPLICATION_ID + ".fileprovider", file);
} else { // Android 5.0 - 6.0
uri = Uri.fromFile(file);
}
::else::
uri = Uri.fromFile(file);
::end::

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Extension.mainActivity.startActivity (intent);

Expand Down

0 comments on commit 42adf86

Please sign in to comment.