How can get the image data encoded base64 from android gallery? the value of base64 is invalid.

Hello. I have a big problem. Please Help me!
First, I’m making android application and i want to make to import image from Android Gallery application. So i’m success to open the gallery using JNI and UPL.

bool FMobileUtilsPlatform::StartGalleryPhotoSelection(UBlueprintAsyncActionBase* ActionPtr)
{
	OnGalleryPhotoSelectedCallbackActionPtr = ActionPtr;
	bool bResult = false;
	if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
	{
		bResult = FJavaWrapper::CallBooleanMethod(Env, FJavaWrapper::GameActivityThis, FMobileUtilsPlatform::StartGalleryPhotoSelectionMethod);
	}
	return bResult;
}


UStartGalleryPhotoSelection* UStartGalleryPhotoSelection::StartGalleryPhotoSelection()
{
	UStartGalleryPhotoSelection* BPNode = NewObject<UStartGalleryPhotoSelection>();
	return BPNode;
}

void UStartGalleryPhotoSelection::Activate()
{
#if PLATFORM_ANDROID
	if (!IMobileUtils::Get().GetPlatformInterface()->StartGalleryPhotoSelection(this))
	{
		Finished.Broadcast("");
		SetReadyToDestroy();
	}
#else
	Finished.Broadcast("");
	SetReadyToDestroy();
#endif
}

extern "C"
{
	JNIEXPORT void Java_com_epicgames_ue4_GameActivity_onGalleryPhotoSelected(JNIEnv* jni, jclass clazz, jstring base64)
	{
		if (OnGalleryPhotoSelectedCallbackActionPtr == nullptr)
		{
			//unknown error
			return;
		}

		ReceiversLock.Lock();
		const char* cstr = jni->GetStringUTFChars(base64, 0);
		FString resultString = FString(cstr);
		jni->ReleaseStringUTFChars(base64, cstr);
		ReceiversLock.Unlock();
		UStartGalleryPhotoSelection* callbackActionPtr = Cast<UStartGalleryPhotoSelection>(OnGalleryPhotoSelectedCallbackActionPtr);
		callbackActionPtr->Finished.Broadcast(resultString);
		callbackActionPtr->SetReadyToDestroy();
	}
}
<gameActivityOnActivityResultAdditions>
		<insert>
			if(requestCode == 1) {
			if(resultCode == Activity.RESULT_OK) {
			Bitmap bitmap;
			try {
			InputStream in = getContentResolver().openInputStream(data.getData());
			bitmap = BitmapFactory.decodeStream(in);
			in.close();
			} catch (IOException e) {
			onGalleryPhotoSelected("");
			return;
			}
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.PNG, 70, byteArrayOutputStream);
			byte[] bytes = byteArrayOutputStream.toByteArray();
			String base64Str = Base64.encodeToString(bytes, Base64.DEFAULT);
			onGalleryPhotoSelected("data:image/png;base64,"+base64Str);
			}
			}
		</insert>
	</gameActivityOnActivityResultAdditions>

<gameActivityClassAdditions>
		<insert>
			private static native void onGalleryPhotoSelected(String base64);

			public boolean AndroidThunkJava_StartGalleryAppImageSelection() {
			Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, 1);
			return true;
			}
</insert>
</gameActivityClassAdditions>

I’d checkout the value of params is valid when i set a short word at onGalleryPhotoSelected. For example onGalleryPhotoSelected("hello"). But these code doesn’t work when i set the encoded base64 value because the FString variable doens’t have whole base64 data. It was disappeared from middle of the data. I don’t know how can i solve this and how can i get the image from the android gallery application?

1 Like