2011-12-09 78 views

回答

0

我使用電暈SDK來開發適用於iOS,但我相信電暈應用程序是沙盒和您無法在沙箱外讀取任何內容。以爲這可能只適用於iOS應用程序。

+0

我認爲üR右..我用Google搜索這個問題,但沒有得到任何合適的回答,我感覺堆棧溢出,少電暈開發商!我在5天只有16view ...這裏很難得到答案.. thnks您的評論..我的意思是答案.. – vnshetty

+0

不用擔心。恐怕Ansca爲了讓Corona在Android和iOS平臺上工作,不得不放棄從沙箱外部讀取文件的支持。我不認爲科羅娜像Cocos2d那樣受歡迎,在這裏得到這樣的關注。你最好在他們的論壇上提問。 – Krystian

6

可以在Android上使用LFS和IO API讀取和寫入SD卡上的文件。

要訪問手機inbuild內存,添加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.