gpt4 book ai didi

android - 如何在 Corona SDK 中从 SD 卡加载图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:34 25 4
gpt4 key购买 nike

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

最佳答案

可以在 Android 上使用 LFS 和 IO API 读取和写入 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.

关于android - 如何在 Corona SDK 中从 SD 卡加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8443291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com