首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在corona sdk中从SD卡中加载图像

如何在corona sdk中从SD卡中加载图像
EN

Stack Overflow用户
提问于 2011-12-09 17:11:44
回答 2查看 2.5K关注 0票数 4

如何使用corona sdk显示SD卡或手机内存中的图像?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-09 20:14:18

我正在使用Corona SDK为iOS开发,但我相信corona应用程序是沙箱的,你不能在沙箱之外阅读任何东西。我认为这可能只适用于iOS应用程序。

票数 0
EN

Stack Overflow用户

发布于 2012-10-20 20:03:06

可以使用Android上的LFS和IO API来读写SD卡上的文件。

要访问手机的内置内存,请添加安卓权限"android.permission.WRITE_EXTERNAL_STORAGE“,并将路径设置为"/”。从那里你可以访问记忆卡。

示例:

代码语言:javascript
运行
复制
local lfs = require("lfs")
local path = "/"
local pathType = ""

-- Check to see if path exists
if path and lfs.attributes( path ) then
    pathType = lfs.attributes( path ).mode
end

-- Check if path is a directory
if pathType == "directory" then
   for file in lfs.dir( path ) do
       if "." ~= file and ".." ~= file then
          -- Get the file attributes.
          local fileAtr = lfs.attributes( path .. "/" .. file )
          -- Print path, name and type of file (Directory or file)
          print(path,file,fileAtr.mode)
       end
    end
end

这将在终端窗口中打印文件的路径、文件名和类型。(仅在Mac和Android设备上测试过。)

我已经找到了一种在Android和模拟器上显示沙箱外的图像的方法。(PC未经过测试)

示例:

代码语言:javascript
运行
复制
local lfs = require("lfs")
    --------------------------- Change this path ---------------------------
local path = "path/to/the/image.jpg"                                    -- Change this path to the path of an image on your computer
------------------------------------------------------------------------


local tmpPath = system.pathForFile("tmp.jpg",system.TemporaryDirectory) -- Destination path to the temporary image

--------------------------- Read ----------------------------
local file, reason = io.open( path, "r" )                               -- Open the image in read mode
local contents
if file then
    contents = file:read( "*a" )                                        -- Read contents
    io.close( file )                                                    -- Close the file (Important!)
else
    print("Invalid path")
    return
end

--------------------------- Write ----------------------------

local file = io.open( tmpPath, "w" )                                    -- Open the destination path in write mode
if file then
    file:write(contents)                                                -- Writes the contents to a file
    io.close(file)                                                      -- Close the file (Important!)
else
    print("Error")
    return
end

---------------------- Open and remove -----------------------
local img = display.newImage("tmp.jpg",system.TemporaryDirectory)       -- Display the image from the temporary directory
os.remove(tmpPath)                                                      -- Removes the image, so that we can load another image.
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8443291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档