一、添加权限

权限添加 :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是一致的

   /storage/emulated/0/Android/data/com.putao.ptx.core/files
 

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文件的更多相关文章

  1. 二十、Android -- SDcard文件读取和保存

    背景                                                                                            一些东西可以 ...

  2. Android -- SDcard文件读取和保存

    背景                                                                                            一些东西可以 ...

  3. Android sdcard文件读写操作

    这次演示以,安卓原生操作系统 Nexus_6手机进行操作: AndroidManifest.xml配置相关权限: <!-- 增加权限 --> <uses-permission and ...

  4. 读取Android APK文件签名的方法

    在微信开放平台等申请API key 和secret时经常要用到apk文件签名,那么如何读取呢? 下面贴一下相关读取源码: 一共两个文件MainActivity和MD5, package com.lcg ...

  5. Android SDCard文件、目录操作【转】

    一.权限问题 参考:http://www.cnblogs.com/sky-zhang/p/3403393.html Android框架是基于Linux内核构建,所以Android安全系统也是基于Lin ...

  6. Unity编译Android的原理解析和apk打包分析

    作者介绍:张坤 最近由于想在Scene的脚本组件中,调用Android的Activity的相关接口,就需要弄明白Scene和Activity的实际对应关系,并对Unity调用Android的部分原理进 ...

  7. Unity使用native读取streamingasset里文件

    需求是,使用native方式,读取apk包里的lua代码,读进c#,做解密 一准备unity工程 public class GameMain : MonoBehaviour { public cons ...

  8. 【转】忙里偷闲写的小例子---读取android根目录下的文件或文件夹

    原文网址:http://www.cnblogs.com/wenjiang/p/3140055.html 最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还 ...

  9. 【转】读取android根目录下的文件或文件夹

    原文网址:http://my.oschina.net/Ccx371161810/blog/287823 读取android根目录下的文件或文件夹 SDK的操作 读取android根目录下的文件或文件夹 ...

随机推荐

  1. SAS常用函数

    SAS常用函数 一.数学函数  ABS(x) 求x的绝对值. MAX(x1,x2,…,xn) 求所有自变量中的最大一个. MIN(x1,x2,…,xn) 求所有自变量中的最小一个. MOD(x,y) ...

  2. 深入详解美团点评CAT跨语言服务监控(九)CAT管理平台MVC框架

    在第2章我们讲到,服务器在初始化CatServlet 之后, 会初始化 MVC,MVC也是继承自AbstractContainerServlet , 同样也是一个 Servlet 容器,这是一个非常古 ...

  3. vue-cli 2.x 项目优化之:引入本地静态库文件

    demo地址:https://github.com/cag2050/vue_cli_optimize_static_resource vue-cli 将静态资源文件放到 static 文件夹下并引用: ...

  4. esp8266尝鲜

    请将当前用户添加到dialout组,否则会提示打开/dev/ttyUSB0权限不足 sudo usermod -a -G dialout `whoami` dmeg查看驱动安装信息 dmesg | g ...

  5. QT编写的网页浏览器网页乱码解决方法

    1.如果是本地网页,可以将网页编码改为GB2312 <meta http-equiv="Content-Type" content="text/html; char ...

  6. Java事件监听器的四种实现方式

    自身类作为事件监听器 外部类作为事件监听器 匿名内部类作为事件监听器 内部类作为事件监听器 自身类作为事件监听器: import javax.swing.*; import java.awt.*; i ...

  7. 同台同时多开DELPHI2007的解决办法

    Cannot create file "C:\Users\Administrator\AppData\Local\Temp\EditorLineEnds.ttr"这个问题的产生根据 ...

  8. 解决Delphi 2010启动时卡死并报“displayNotification: 堆栈溢出”错误

    1. 清理IE的历史记录,删除浏览器缓存(不需要清cookie) 2. 禁用startpage 2.1 从 Delphi 2010 启动菜单上点右键 -> 查看属性->快捷方式->目 ...

  9. Maven 专题

    目录: Maven的安装 Eclipse安装Maven插件 Nexus私服搭建 Maven+Nexus配置 发布自己的构件(至Nexus) 创建maven多模块工程group 整理中[...] 先放一 ...

  10. 【Mysql】事务日志-Write Ahead logging vs command-logging(转)

    原理讲解: Write Ahead logging vs command logging Write Ahead logging 持久化数据保存在磁盘,数据的存储是随机的,并非顺序: 内存中保存磁盘数 ...