Unity读取Android SDcard文件
一、添加权限
权限添加 :Player settings -- Other settings -- write permission的设置 Sdcard。这个是在Unity编辑器里打包的情况。
如果导出到studio 里面的话,可自行修改Manifest文件。
二、两种方式
IO方式 加载sdcard上的图片资源
加载的 /storage/emulated/0/ProjectName/image.jpg,
image = this.GetComponentInChildren<Image>();
Debug.Log("IO加载用时: image = this.GetComponent<Image> ========== " + image);
Debug.Log("IO加载: Application.dataPath " + Application.dataPath );
// Application.dataPath /data/app/com.putao.ptx.core-1/base.apk
Debug.Log("IO加载: Application.persistentDataPath " + Application.persistentDataPath);
// Application.persistentDataPath /storage/emulated/0/Android/data/com.putao.ptx.core/files
Debug.Log("IO加载:GameObject.Find" + GameObject.Find("Canvas/Avator").GetComponent<Image>());
// /data/user/0/com.putao.paichallenge/cache/20170524_130527.jpg
// path ======= "/storage/emulated/0/ProjectName/image.jpg"
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//创建Texture
int width = 300;
int height = 372;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
//创建Sprite
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width,texture.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite;
log上可以看出来 android上 getExternalFilesDir("") 和 Unity里面的Application.persistentDataPath是一致的
WWW方式加载本地图片
url= "file://"+ "/storage/emulated/0/ProjectName/image.jpg"
根本不是加"jar:file://..."
public void LoadByWWW(String path)
{
StartCoroutine(doLoadByWWW(path)); } IEnumerator doLoadByWWW(String path)
{
string url = "file://" + "/storage/emulated/0/PaiChallenge/image.jpg";
Debug.Log("doLoadByWWW == url ================== " + url); WWW w = new WWW(url); yield return w; if (w.isDone)
{
Sprite sprite = Sprite.Create(w.texture, new Rect(0, 0, w.texture.width, w.texture.height), new Vector2(0.5f, 0.5f)); GameObject.Find("Canvas/Avator").GetComponent<Image>().sprite = sprite; }
Unity读取Android SDcard文件的更多相关文章
- 二十、Android -- SDcard文件读取和保存
背景 一些东西可以 ...
- Android -- SDcard文件读取和保存
背景 一些东西可以 ...
- Android sdcard文件读写操作
这次演示以,安卓原生操作系统 Nexus_6手机进行操作: AndroidManifest.xml配置相关权限: <!-- 增加权限 --> <uses-permission and ...
- 读取Android APK文件签名的方法
在微信开放平台等申请API key 和secret时经常要用到apk文件签名,那么如何读取呢? 下面贴一下相关读取源码: 一共两个文件MainActivity和MD5, package com.lcg ...
- Android SDCard文件、目录操作【转】
一.权限问题 参考:http://www.cnblogs.com/sky-zhang/p/3403393.html Android框架是基于Linux内核构建,所以Android安全系统也是基于Lin ...
- Unity编译Android的原理解析和apk打包分析
作者介绍:张坤 最近由于想在Scene的脚本组件中,调用Android的Activity的相关接口,就需要弄明白Scene和Activity的实际对应关系,并对Unity调用Android的部分原理进 ...
- Unity使用native读取streamingasset里文件
需求是,使用native方式,读取apk包里的lua代码,读进c#,做解密 一准备unity工程 public class GameMain : MonoBehaviour { public cons ...
- 【转】忙里偷闲写的小例子---读取android根目录下的文件或文件夹
原文网址:http://www.cnblogs.com/wenjiang/p/3140055.html 最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还 ...
- 【转】读取android根目录下的文件或文件夹
原文网址:http://my.oschina.net/Ccx371161810/blog/287823 读取android根目录下的文件或文件夹 SDK的操作 读取android根目录下的文件或文件夹 ...
随机推荐
- nginx配置.htaccess伪静态
https://blog.csdn.net/moqiang02/article/details/37695775
- 我 支持 使用 async await
这篇文章原来的 标题 是 <我 反对 使用 async await>, 但经过后来的一些研究, 发现 async await 是 良性 的, 所以 我把 标题 改成了 <我 支持 使 ...
- css 兼容性问题,整理
css 兼容性问题,整理: css 兼容性问题 说明 <input type="number"> 在chrome下,是不能输入非数字的字符的:但是在火狐63.0.3(2 ...
- TypeScript 之 声明文件的结构
https://www.tslang.cn/docs/handbook/declaration-files/library-structures.html 模块化库 一些库只能工作在模块加载器的环境下 ...
- CentOS6.5 安装+ Tengine + PHP + MySQL
简介: Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了 ...
- ueditor图片上传和显示问题
图片上传: 这段是contorller代码 @RequestMapping(value = "/uploadImg", method = RequestMethod.POST) @ ...
- [转]【NLP】干货!Python NLTK结合stanford NLP工具包进行文本处理 阅读目录
[NLP]干货!Python NLTK结合stanford NLP工具包进行文本处理 原贴: https://www.cnblogs.com/baiboy/p/nltk1.html 阅读目录 目 ...
- innobackupex per table
#innobackupex export tableinnobackupex --defaults-file=/etc/my.cnf --sock=/data/3306/mysql_3306.sock ...
- 关于使用MAPVIEWOFFILE大文件的读写(DELPHI版)
unit filemap; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- https 不检验证书
System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErro ...