一、添加权限

权限添加 :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. Gravitational Teleport简单使用

    使用官方提供的二进制包进行快速启动测试,详细细节还需要在学习 下载软件包 mac 系统 https://gravitational.com/teleport/download/ wget https: ...

  2. Compoxure example 应用说明

    Compoxure 官方提供了一个demo应用,包含了cache,error,layout 等功能 环境准备 demo 使用docker-compose 运行 clone 代码 git clone h ...

  3. js技巧专题篇: 页面跳转

    本篇主要介绍网页上常见的页面跳转技术.页面跳转有几种方式,比较常用的是window.location.href,window.location.replace,window.open,当然还有目前比较 ...

  4. Primitives vs Objects

    这里首先我们要了解什么是primitives 和 objects 其实理解起来很简单. 如果我们懂.NET开发就会知道C#中的值类型和引用类型. primitives variables contai ...

  5. 我发起了一个 支持 PostgreSql 的 外围设施 的 .Net 开源项目

    目标 :  让 PostgreSql 成为 通用的 跨平台 的 数据库 , 成为 开发者 喜爱 的 利器 . 要做的事 , 当然 , PostgreSql 本身现在不用我们去做什么 . 一个 数据库 ...

  6. 排序算法<No.3>【桶排序】

    算法,是永恒的技能,今天继续算法篇,将研究桶排序. 算法思想: 桶排序,其思想非常简单易懂,就是是将一个数据表分割成许多小数据集,每个数据集对应于一个新的集合(也就是所谓的桶bucket),然后每个b ...

  7. 关于vivado----xdc文件时钟约束的初识

    关于vivado----xdc文件时钟约束的初识 1.Primary Clocks(主时钟) 然而,对于比较复杂的时钟: 2.某个模块采用的主时钟(比如说GT) 3.时钟分频 4.复杂一点的时钟描述 ...

  8. 黄聪:is_file和file_exists效率比较

    目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧: 1 2 ...

  9. 在已有Windows系统基础上安装ubuntu后无Windows启动项

    1. 原系统为windows 2. 清出一个不用的分区给linux,使用windows磁盘管理器将该分区重新划分为两个分区,分别用于root和swap 3. 安装过程中ubuntu提示原系统为非UEF ...

  10. DS二叉树--Huffman编码与解码

    题目描述 1.问题描述 给定n个字符及其对应的权值,构造Huffman树,并进行huffman编码和译(解)码. 构造Huffman树时,要求左子树根的权值小于.等于右子树根的权值. 进行Huffma ...