Android 如何在corona sdk中从SD卡加载图像
如何使用corona sdk显示SD卡或手机内存中的图像?`我使用corona sdk为iOS开发,但我认为corona应用程序是沙盒,您无法读取沙盒之外的任何内容。我认为这可能只适用于iOS应用程序。在Android上使用LFS和IO API在SD卡上读写文件是可能的 要访问内置内存中的手机,请添加android权限“android.permission.WRITE_EXTERNAL_STORAGE”,并将路径设置为“/”。从那里你可以访问记忆卡 例如:Android 如何在corona sdk中从SD卡加载图像,android,coronasdk,Android,Coronasdk,如何使用corona sdk显示SD卡或手机内存中的图像?`我使用corona sdk为iOS开发,但我认为corona应用程序是沙盒,您无法读取沙盒之外的任何内容。我认为这可能只适用于iOS应用程序。在Android上使用LFS和IO API在SD卡上读写文件是可能的 要访问内置内存中的手机,请添加android权限“android.permission.WRITE_EXTERNAL_STORAGE”,并将路径设置为“/”。从那里你可以访问记忆卡 例如: local lfs = require
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
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.
这将在终端窗口中打印文件的路径、文件名和类型。(仅在Mac和Android设备上进行了测试。)
我找到了一种在Android和模拟器上在沙箱外显示图像的方法。(PC未测试)
例如:
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
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.
我想你是对的。。我在谷歌上搜索了这个问题,但没有得到任何正确的答案,我觉得堆栈溢出的影响更小!我在5天内只看了16次。。。这里很难找到答案。。谢谢你的评论。。我的意思是回答……不用担心。恐怕Ansca为了使Corona在Android和iOS平台上都能工作,不得不放弃对从沙箱外部读取文件的支持。我不认为科罗纳像Cocos2d那样受欢迎,在这里得到如此多的关注。你最好在他们的论坛上提问。