本文旨在介绍,如何将游戏内的图片保存到Android相册,并实时刷新显示出来(不需要写Android原生项目),另外解决存储权限申请和弹窗隐藏问题
- Internal 是内部存储,无需存储权限,可以使用软件目录下的存储,但不可以访问手机目录(如果我们只是想保存图片到相册,选择该方式即可)
- External(SD) 是外部存储,需要存储权限。(但是从产品的角度考虑,弹窗的转化率是非常的低,大多数用户是不愿意同意该权限申请)
打开图片的读写功能
File写入贴图时,需要将图片转成字节数组,texture2D.EncodeToPNG(),但是会出现如下报错
传入目标贴图,返回的图片即可转成字节数组啦
public Texture2D DeCompress(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
///
/// 保存图片
///
///
///
private void SaveImages(Texture2D texture)
{
string path = Application.streamingAssetsPath;
#if UNITY_ANDROID && !UNITY_EDITOR
path = "/sdcard/DCIM/Camera"; //设置图片保存到设备的目录.
#endif
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string savePath = path + "/" + m_DailyBgConfigData.Maintitle + ".png";
try
{
Application.HasUserAuthorization(UserAuthorization.Microphone);
byte[] data = DeCompress(texture).EncodeToPNG();
File.WriteAllBytes(savePath, data);
OnSaveImagesPlartform(savePath);
}
catch
{
}
}
///
/// 刷新相册(不需要单独创建原生aar或jar)
///
///
private void OnSaveImagesPlartform(string filePath)
{
#if UNITY_ANDROID && !UNITY_EDITOR
string[] paths = new string[1];
paths[0] = filePath;
using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
{
Conn.CallStatic("scanFile", playerActivity, paths, null, null);
}
}
#endif
}
///
/// 压缩图片
///
///
///
public Texture2D DeCompress(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
这里主要说下刷新相册的方法,如果我们不调用刷新的haul,图片是可以在文件管理器中找到的,但是并不会显示在相册中,MediaScannerConnection.scanFile是Android原生的接口,我们不再需要单独再去创建个AS项目,打个aar包
Plugin->Android->AndroidManifest.xml中添加如写入权限
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
比如用户点击下载时,先行判断是否写入权限,没有的话就直接申请好啦
if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.ExternalStorageWrite))
{
UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.ExternalStorageWrite);
return;
}