【UE4】下载URL图片保存到本地

41 篇文章 3 订阅

     大家好我是Ning+,因为工作需要顺手写了个简单的下载与保存,主要要注意的地方是URLDownloadToFile形参需要_T()修饰的ANSI参数,这里我将FString转为String,然后String转为wchar_T 即可;

/*
@author:Ning+
@theme:URL下载图片 2020/04/01
*/
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"

#include <tchar.h>
#include <iostream>
#include <urlmon.h>

#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"

/*
#pragma comment(lib,"urlmon.lib") MS静态链接库:
*/
#pragma comment(lib,"urlmon.lib")
using namespace std;


//从指定URL下载瓦片
bool Uclass::DownLoadIMG(FString URL, FString SaveSource, FString SaveName)
{
	
	string url = TCHAR_TO_UTF8(*URL);
	size_t len = url.length();//获取字符串长度
	int nmlen = MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, NULL, 0);//如果函数运行成功,并且cchWideChar为零,
	//返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
	wchar_t* buffer = new wchar_t[nmlen];
	MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, buffer, nmlen);
	
	//转换为ASNI
	string str = TCHAR_TO_UTF8(*SaveSource);
	str = str + TCHAR_TO_UTF8(*SaveName);

	std::wstring widstr = std::wstring(str.begin(), str.end());
	const wchar_t *pwidstr = widstr.c_str();

	HRESULT hr = URLDownloadToFile(NULL, buffer, pwidstr, 0, NULL);

	if (hr == S_OK)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

 

UE4保存图片到Android相册,可以使用Java Native Interface(JNI)来实现。下面是具体的步骤: 1. 首先,创建一个Blueprint Function Library。在Blueprint Function Library中创建一个名为"SaveImageToGallery"的静态函数。 2. 打开Visual Studio,并将C++代码添加到BPFunctionLibrary.h和BPFunctionLibrary.cpp文件中。代码如下: // BPFunctionLibrary.h #pragma once #include "Kismet/BlueprintFunctionLibrary.h" #include "BPFunctionLibrary.generated.h" UCLASS() class YOURPROJECT_API UBPFunctionLibrary : public UBlueprintFunctionLibrary { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category = "MyFunctions") static bool SaveImageToGallery(FString FilePath); }; // BPFunctionLibrary.cpp #include "YourProject.h" #include "BPFunctionLibrary.h" #include "Android/AndroidJNI.h" #include "Android/AndroidApplication.h" bool UBPFunctionLibrary::SaveImageToGallery(FString FilePath) { JNIEnv* Env = FAndroidApplication::GetJavaEnv(); if (Env == nullptr) { return false; } FString ClassName = FString("com/epicgames/ue4/GameActivity"); jclass ClassID = Env->FindClass(TCHAR_TO_UTF8(*ClassName)); if (ClassID == nullptr) { return false; } jmethodID MethodID = Env->GetStaticMethodID(ClassID, "SaveImageToGallery", "(Ljava/lang/String;)V"); if (MethodID == nullptr) { return false; } jstring Filename = Env->NewStringUTF(TCHAR_TO_UTF8(*FilePath)); Env->CallStaticVoidMethod(ClassID, MethodID, Filename); Env->DeleteLocalRef(Filename); return true; } 3. 在Java项目中,创建一个名为"GameActivity"的类,并在该类中添加一个名为"SaveImageToGallery"的静态方法。代码如下: package com.epicgames.ue4; import android.content.ContentValues; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.provider.MediaStore; import java.io.File; import java.io.FileOutputStream; public class GameActivity extends android.app.NativeActivity { static void SaveImageToGallery(String FilePath) { File ImageFile = new File(FilePath); Bitmap imageBitmap = BitmapFactory.decodeFile(ImageFile.getAbsolutePath()); ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); values.put(MediaStore.Images.Media.MIME_TYPE, "image/png"); values.put(MediaStore.Images.Media.ORIENTATION, 0); values.put(MediaStore.MediaColumns.DATA, ImageFile.getAbsolutePath()); ContentResolver contentResolver = this.getContentResolver(); Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); try { FileOutputStream fos = contentResolver.openOutputStream(uri); if (fos != null) { imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } } catch (Exception e) { e.printStackTrace(); } } } 4. 现在我们可以在UE4中的蓝图中调用"SaveImageToGallery"函数来保存图片到Android相册了。只需传递要保存图片文件路径即可。 这样,当我们在UE4中运行该蓝图,并调用"SaveImageToGallery"函数时,图片将被保存到Android的相册中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值