关于Android无法在Application.persistentDataPath中加载json文件

时间:2019-08-16 07:54:35

标签: android json unity3d

我将json文件放在Application.persistentDataPath中,然后就可以在PC上加载和工作了。

但是它不能在Android中工作,我被困了很长时间。

我找到了很多方法,但是没人能解决它。

请帮助我,谢谢!

class ChatViewController {

    struct dateModelStructure: Hashable {
        var date: String
        var contents: String

        static func ==(lhs: ChatViewController.dateModelStructure, rhs: ChatViewController.dateModelStructure) -> Bool {
            return lhs.date == rhs.date && lhs.contents == rhs.contents
        }
    }

    var messagesPerDateDictionary: [dateModelStructure] = []
    var dateSection = [Any]()
}

var someData = [
    [ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?")],
    [ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"),ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?")],
    [ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?"), ChatViewController.dateModelStructure(date: "2019-08-13", contents: "Thank you")],
    [ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20")],
    [ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20"), ChatViewController.dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_movies%2F155A8712-F437-4B3E-AE76-97ECA1C3A4B6.mov?alt=media&token=a12c4f7f-d46a-4109-ace0-97bdacaf04cf")],
    [ChatViewController.dateModelStructure(date: "2019-08-15", contents: "Counts")]
]

var helperDict = [String: [ChatViewController.dateModelStructure]]()

someData.forEach { (data) in
    guard let firstDate = data.first?.date else {
        return
    }
    if let existingArray = helperDict[firstDate] {
        helperDict[firstDate] = existingArray + data
    } else {
        helperDict[firstDate] = data
    }
}

var filtered : [[ChatViewController.dateModelStructure]] = helperDict.values.reduce([[ChatViewController.dateModelStructure]]()) { (previous, current) -> [[ChatViewController.dateModelStructure]] in
    var next = [Array(Set(current))] + previous
    return next
}



filtered.forEach { (items) in
    print ("------------------------------")
    items.forEach({ (item) in
        print(item)
    })
}

------------------------------
dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_images%2FDF89F565-9BDB-442F-83B4-B5D6E0BD0739?alt=media&token=9d54f505-50ce-45f5-999e-2d7c6981ec20")
dateModelStructure(date: "2019-08-14", contents: "https://firebasestorage.googleapis.com/v0/b/tikitalka-a8e51.appspot.com/o/message_movies%2F155A8712-F437-4B3E-AE76-97ECA1C3A4B6.mov?alt=media&token=a12c4f7f-d46a-4109-ace0-97bdacaf04cf")
------------------------------
dateModelStructure(date: "2019-08-13", contents: "Why did you call me a fat cat?")
dateModelStructure(date: "2019-08-13", contents: "Um...my chat..where did it go?")
dateModelStructure(date: "2019-08-13", contents: "Thank you")
------------------------------
dateModelStructure(date: "2019-08-15", contents: "Counts")

1 个答案:

答案 0 :(得分:0)

尝试将文件写入外部(SDCard)而不是内部文件。

您可以在PlayerSettings-> Android

下更改此设置

External SD Card Setting