如何在Corona SDK中从SD卡加载图片

4
我该如何使用corona sdk从sd卡或手机内存中显示图片?
2个回答

6

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

要访问手机内置存储器,请添加Android权限“android.permission.WRITE_EXTERNAL_STORAGE”,并将路径设置为“/”。从那里,您可以访问内存卡。

示例:

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上测试)

例如:

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.

0

我正在使用Corona SDK开发iOS应用,但我相信Corona应用程序是沙盒化的,您无法读取沙盒之外的任何内容。虽然这可能只适用于iOS应用程序。


我认为你是对的。我在谷歌上搜索了这个问题,但没有得到任何恰当的答案,我感觉堆栈溢出上缺少冠状病毒开发者!我在5天内只有16次浏览量... 在这里很难得到答案... 感谢你的评论... 我的意思是答案... - vnshetty
不用担心。我担心Ansca为了让Corona在Android和iOS平台上运行,不得不放弃支持从沙盒外读取文件的功能。我认为Corona并不像Cocos2d那样受欢迎,无法在这里获得如此关注。你最好在他们的论坛上提问。 - Krystian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接