最近,我在尝试将文件上传到Android上的存储空间时遇到了问题。它在Unity编辑器上运行得非常好,但每当我在Android上运行它时,我会不断收到此错误:
System.AggregateException:类型' System.AggregateException'的异常被扔了。 Firebase.Storage.StorageException:没有内容提供者: /storage/emulated/0/Android/data/com.Comp.App/files/Uploads/ppp/master.json
以下是我用来将文件上传到存储空间的代码:
Firebase.Storage.StorageReference f_ref = storage_ref.Child(referencePath);
print("Reference path: " + referencePath);
// Upload the file
f_ref.PutFileAsync(filePath)
.ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.Log(task.Exception.ToString());
print("Couldn't upload " + filePath);
// Uh-oh, an error occurred!
}
else {
Firebase.Storage.StorageMetadata metadata = task.Result;
string download_url = metadata.DownloadUrl.ToString();
Debug.Log("download url = " + download_url);
}
});
该文件存储在依赖于设备的永久文件路径中。我试图有目的地上传一个不存在的文件路径,我收到了一个&#34; FileNotFound&#34;异常,所以我确定我尝试上传的文件的路径是正确的。
我非常感谢任何帮助,找出这个异常的含义以及如何解决它。提前谢谢!
答案 0 :(得分:1)
使用inputStream
和putStream
代替:
InputStream is = new FileInputStream(filePath);
f_ref.putStream(is).....;
答案 1 :(得分:0)
也许您应该传递Like This
形式的 URI文件:/// SD卡/ yourFilePath
答案 2 :(得分:0)
我也遇到了同样的问题,E。Abdel的回答使我走上了正确的轨道。我试图上传用BinaryFormatter
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + pathToFile);
bf.Serialize(file, currentPlayer);
file.Close();
我遇到了完全相同的错误,然后尝试了另一种上载错误的方式。代替PutFileAsync
,使用PutStreamAsync
,我想这需要用于带有BinaryFormatter
的序列化文件。
// File located on disk
string local_file = Application.persistentDataPath + pathToFile;
// Create a reference to the file you want to upload
StorageReference storage_ref = FirebaseStorage.DefaultInstance.RootReference;
StorageReference player_ref = storage_ref.Child("test");
Stream stream = new FileStream(local_file, FileMode.Open);
player_ref.PutStreamAsync(stream).ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.Log(task.Exception.ToString());
// Uh-oh, an error occurred!
}
else
{
// Success
}
});
答案 3 :(得分:0)
在我的情况下,我遇到了同样的错误(Firebase.Storage.StorageException:无内容提供者),只是为了使用 Android 上传文件。
阅读 E. Abdel 的回答,我在 C# 到 Unity 中找到了以下解决方案。
StreamReader stream = new StreamReader(file_path);
StorageReference storage_ref = storageRef.Child(file_name_to_storage);
storage_ref.PutStreamAsync(stream.BaseStream)
.ContinueWith((Task<StorageMetadata> task) => {
if (task.IsFaulted || task.IsCanceled)
{
Debug.Log(task.Exception.ToString());
}
else
{
Debug.Log("Finished uploading...");
}
});
它可以在 Unity 编辑器和 Android 中上传指定文件路径的文件,在我的情况下,file_path 是:/storage/emulated/0/file_name.png