Merge pull request #72496 from m4gr3d/implement_file_provider_3x
[3.x] Implement file provider capabilities
This commit is contained in:
commit
7722461dc5
4 changed files with 52 additions and 11 deletions
|
@ -968,6 +968,10 @@ void EditorExportPlatformAndroid::_fix_manifest(const Ref<EditorExportPreset> &p
|
||||||
encode_uint32(is_resizeable, &p_manifest.write[iofs + 16]);
|
encode_uint32(is_resizeable, &p_manifest.write[iofs + 16]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tname == "provider" && attrname == "authorities") {
|
||||||
|
string_table.write[attr_value] = get_package_name(package_name) + String(".fileprovider");
|
||||||
|
}
|
||||||
|
|
||||||
if (tname == "supports-screens") {
|
if (tname == "supports-screens") {
|
||||||
if (attrname == "smallScreens") {
|
if (attrname == "smallScreens") {
|
||||||
encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
|
encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]);
|
||||||
|
|
|
@ -20,6 +20,16 @@
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name="androidx.core.content.FileProvider"
|
||||||
|
android:authorities="${applicationId}.fileprovider"
|
||||||
|
android:exported="false"
|
||||||
|
android:grantUriPermissions="true">
|
||||||
|
<meta-data
|
||||||
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||||
|
android:resource="@xml/godot_provider_paths" />
|
||||||
|
</provider>
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
11
platform/android/java/lib/res/xml/godot_provider_paths.xml
Normal file
11
platform/android/java/lib/res/xml/godot_provider_paths.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<external-path
|
||||||
|
name="public"
|
||||||
|
path="." />
|
||||||
|
|
||||||
|
<external-files-path
|
||||||
|
name="app"
|
||||||
|
path="." />
|
||||||
|
</paths>
|
|
@ -49,6 +49,9 @@ import android.view.Display;
|
||||||
import android.view.DisplayCutout;
|
import android.view.DisplayCutout;
|
||||||
import android.view.WindowInsets;
|
import android.view.WindowInsets;
|
||||||
|
|
||||||
|
import androidx.core.content.FileProvider;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
@ -84,29 +87,42 @@ public class GodotIO {
|
||||||
// MISCELLANEOUS OS IO
|
// MISCELLANEOUS OS IO
|
||||||
/////////////////////////
|
/////////////////////////
|
||||||
|
|
||||||
public int openURI(String p_uri) {
|
public int openURI(String uriString) {
|
||||||
try {
|
try {
|
||||||
String path = p_uri;
|
Uri dataUri;
|
||||||
String type = "";
|
String dataType = "";
|
||||||
if (path.startsWith("/")) {
|
boolean grantReadUriPermission = false;
|
||||||
//absolute path to filesystem, prepend file://
|
|
||||||
path = "file://" + path;
|
if (uriString.startsWith("/") || uriString.startsWith("file://")) {
|
||||||
if (p_uri.endsWith(".png") || p_uri.endsWith(".jpg") || p_uri.endsWith(".gif") || p_uri.endsWith(".webp")) {
|
String filePath = uriString;
|
||||||
type = "image/*";
|
// File uris needs to be provided via the FileProvider
|
||||||
|
grantReadUriPermission = true;
|
||||||
|
if (filePath.startsWith("file://")) {
|
||||||
|
filePath = filePath.replace("file://", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File targetFile = new File(filePath);
|
||||||
|
dataUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", targetFile);
|
||||||
|
dataType = activity.getContentResolver().getType(dataUri);
|
||||||
|
} else {
|
||||||
|
dataUri = Uri.parse(uriString);
|
||||||
}
|
}
|
||||||
|
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setAction(Intent.ACTION_VIEW);
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
if (!type.equals("")) {
|
if (TextUtils.isEmpty(dataType)) {
|
||||||
intent.setDataAndType(Uri.parse(path), type);
|
intent.setData(dataUri);
|
||||||
} else {
|
} else {
|
||||||
intent.setData(Uri.parse(path));
|
intent.setDataAndType(dataUri, dataType);
|
||||||
|
}
|
||||||
|
if (grantReadUriPermission) {
|
||||||
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
activity.startActivity(intent);
|
activity.startActivity(intent);
|
||||||
return 0;
|
return 0;
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
|
Log.e(TAG, "Unable to open uri " + uriString, e);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue