最近近到个小需求,需要读Android件夹中的图片.在这里做一个记录.
首先读写部分,这里以图片为例子:
一读写部分
写入部分:
需要注意的是因为只有这个地址支持外部读写,所以这里用到的地址都以 :Application.persistentDataPath
为地址起始.
private Texture2D __CaptureCamera()
{
RenderTexture rt = m_renGet;
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D(256, 256, TextureFormat.RGB24, false);
screenShot.ReadPixels(new Rect(0, 0, 256, 256), 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.persistentDataPath + "/House1.jpg";
System.IO.File.WriteAllBytes(filename, bytes);
return screenShot;
}
读取部分:
为了方便测试这里使用了InputFiled组件为了方便随时调整地址:
private void __On3()
{
input.text = Application.persistentDataPath;
string path = "file:///" + input.text;
StartCoroutine(LoadImage(path, bb));
}
private void bb(object obj)
{
Texture2D tex = obj as Texture2D;
rw.texture = tex;
}
public IEnumerator LoadImage(string path, Action<object> callback)
{
Debug.Log("读取地址:"+path);
using (WWW www = new WWW(path))
{
yield return www;
if (www.error != null)
{
if (callback != null)
{
//加载失败将加载本地默认图片
//callback(Resources.Load(AssetPathConfig.DEFAULT_IMAGE) as Texture2D);
//Debug.Log("失败");
Debug.Log("失败");
}
}
else
{
if (www.isDone)
{
if (callback != null)
{
Debug.Log("成功");
callback(www.texture);
}
}
}
}
}
二权限申请
权限获取分为三个部分
版本设置
需要将Android调整到较高版本(不一定)
外部读写设置
然后是将Project Setting > OtherSettings > Write permission 设置为 Extemal(SDCard)
使用可以方位指定文件夹外的文件
权限配置
然后勾选Project Setting > Publishing Settings > Build > Custom Main Manifest
此时将在自动在Plugins 下生成Android/AndroidManifest文件
在些可添加额外的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:theme="@style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
申请权限
只需要在游戏开始调用RequestPermissions
方法即可完成权限申请
//申请多个权限
private void RequestPermissions()
{
if (m_StringBuilder == null)
{
m_StringBuilder = new StringBuilder();
}
else
{
m_StringBuilder.Clear();
}
m_IsGetAllPermission = false;
//申请回调
m_PermissionCallbacks = new PermissionCallbacks();
m_PermissionCallbacks.PermissionDenied += OnPermissionDenied;
m_PermissionCallbacks.PermissionGranted += OnPermissionGranted;
m_PermissionCallbacks.PermissionDeniedAndDontAskAgain += OnPermissionDeniedAndDontAskAgain;
//申请权限组
string[] permissions =
{
Permission.ExternalStorageRead,
Permission.ExternalStorageWrite,
// 添加其他权限
};
//执行申请多个权限
Permission.RequestUserPermissions(permissions, m_PermissionCallbacks);
}
//申请权限被拒绝
private void OnPermissionDenied(string permission)
{
Debug.Log($"OnPermissionDenied:{permission}");
}
//申请权限成功
/// <param name="permission"></param>
private void OnPermissionGranted(string permission)
{
Debug.Log($"OnPermissionGranted:{permission}");
//检查权限是否全部通过
if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead) &&
Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
//防止多次回调
if (!m_IsGetAllPermission)
{
m_IsGetAllPermission = true;
//在这里处理权限通过的逻辑
//do something
Debug.Log("权限申请全部通过");
}
}
}
//申请权限被拒绝,且不再询问
private void OnPermissionDeniedAndDontAskAgain(string permission)
{
Debug.Log ($"OnPermissionDeniedAndDontAskAgain:{permission}");
}
四测试
此时我在手机根目录下放一张图片22.jpg
我们读取地址 “file:///” + Application.persistentDataPath + “/…/…/…/…/22.jpg”
即可以获得此照片../
是上一级目录的意思.
好啦,此次分享至此结束