Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GodotGetImage Plugin Godot 3.2.4 patch
diff --git a/GodotGetImagePlugin/godotgetimage/src/main/java/com/gmail/lamelynx/godotgetimage/GodotGetImage.kt b/GodotGetImagePlugin/godotgetimage/src/main/java/com/gmail/lamelynx/godotgetimage/GodotGetImage.kt
index 3414ea8..7f9038c 100644
--- a/GodotGetImagePlugin/godotgetimage/src/main/java/com/gmail/lamelynx/godotgetimage/GodotGetImage.kt
+++ b/GodotGetImagePlugin/godotgetimage/src/main/java/com/gmail/lamelynx/godotgetimage/GodotGetImage.kt
@@ -32,7 +32,7 @@ import java.io.InputStream
* Mail: lamelynx@gmail.com
*/
-class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
+class GodotGetImage(godot: Godot) : GodotPlugin(godot) {
private val TAG: String = "godot"
private val REQUEST_GALLERY_IMAGE_ID = 101
@@ -130,7 +130,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
val intent = Intent(Intent.ACTION_PICK)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.type = "image/*"
- activity?.startActivityForResult(intent, REQUEST_GALLERY_IMAGE_ID)
+ godot?.startActivityForResult(intent, REQUEST_GALLERY_IMAGE_ID)
}
}
@@ -147,7 +147,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.type = "image/*"
- activity?.startActivityForResult(intent, REQUEST_GALLERY_IMAGE_ID)
+ godot?.startActivityForResult(intent, REQUEST_GALLERY_IMAGE_ID)
}
}
@@ -158,12 +158,14 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
*/
Log.d(TAG, "Call - getCameraImage")
+ var context = godot.context
+
// Check permission
if (getPermission(Manifest.permission.CAMERA)) {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
// Ensure that there's a camera activity to handle the intent
- takePictureIntent.resolveActivity(godot.packageManager)?.also {
+ takePictureIntent.resolveActivity(context?.packageManager!!)?.also {
// Create the File where the photo should go
val photoFile: File? = try {
createImageFile()
@@ -175,8 +177,8 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
// Continue only if the File was successfully created
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
- godot,
- godot.packageName,
+ context,
+ context.packageName,
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
@@ -286,7 +288,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
// Load image without any max size. May cause 'out of memory' on big images
val opt = BitmapFactory.Options()
opt.inPreferredConfig = Bitmap.Config.ARGB_8888
- val inputImage = godot.contentResolver.openInputStream(uri)
+ val inputImage = activity?.contentResolver?.openInputStream(uri)
bitmap = BitmapFactory.decodeStream(inputImage, null, opt)!!
inputImage?.close()
}
@@ -348,15 +350,16 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
* @return true if permission is already granted
*/
var ret = false
+ var context = godot.context
if (ContextCompat.checkSelfPermission(
- godot,
+ context!!,
permission
) != PackageManager.PERMISSION_GRANTED
) {
Log.d(TAG, "Application has not permission: $permission")
if (ActivityCompat.shouldShowRequestPermissionRationale(
- godot,
+ activity!!,
permission
)
) {
@@ -365,7 +368,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
if (resendPermission) {
resendPermission = false
ActivityCompat.requestPermissions(
- godot,
+ activity!!,
arrayOf(permission),
REQUEST_PERMISSION_ID
)
@@ -375,7 +378,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(
- godot,
+ activity!!,
arrayOf(permission),
REQUEST_PERMISSION_ID
)
@@ -390,7 +393,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
- val storageDir: File? = godot.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
+ val storageDir: File? = activity?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"tmpImage1133", /* prefix */
".$imageFormat", /* suffix */
@@ -431,7 +434,7 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
// Preload without actually load image
inJustDecodeBounds = true
inPreferredConfig = Bitmap.Config.ARGB_8888
- var input: InputStream? = godot.contentResolver.openInputStream(uri)
+ var input: InputStream? = activity?.contentResolver?.openInputStream(uri)
BitmapFactory.decodeStream(input, null, this)
input?.close()
@@ -440,10 +443,10 @@ class GodotGetImage(activity: Godot) : GodotPlugin(activity) {
// Decode bitmap with inSampleSize set
inJustDecodeBounds = false
- input = godot.contentResolver.openInputStream(uri)
+ input = activity?.contentResolver?.openInputStream(uri)
val bitmap: Bitmap = BitmapFactory.decodeStream(input, null, this) as Bitmap
input?.close()
bitmap
}
}
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment