我是Android和Firebase的新手。基本上,我正在尝试从Firebase下载文件并将其存储在Android存储中,但我收到了一个奇怪的错误"ENOENT: No such file or directory error"
。我也有写入存储权限。
代码:
public class MainActivity extends AppCompatActivity
{
private StorageReference mStorageRef;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorageRef = FirebaseStorage.getInstance().getReference().child("20JAN2017.txt");
System.out.println("......"+mStorageRef);
Uri url;
mStorageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri)
{
FileDownloadTask file = mStorageRef.getFile(uri);
file.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
System.out.println("task snapshot..."+taskSnapshot.getStorage());
}
});
file.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.out.println("Error:"+e);
}
});
file.addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
System.out.println("task completed");
}
});
}
});
}
}
错误:
01-23 21:53:19.502 10974-11033/indian.stock.screener E/FileDownloadTask: Exception occurred during file download
java.io.IOException: open failed: ENOENT (No such file or directory)
at java.io.File.createNewFile(File.java:946)
at com.google.firebase.storage.FileDownloadTask.run(Unknown Source)
at com.google.firebase.storage.StorageTask$8.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at java.io.File.createNewFile(File.java:939)
at com.google.firebase.storage.FileDownloadTask.run(Unknown Source)
at com.google.firebase.storage.StorageTask$8.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
01-23 21:53:19.712 10974-11033/indian.stock.screener E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 200
非常感谢任何帮助。
答案 0 :(得分:0)
mStorageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
FileDownloadTask file = mStorageRef.getFile(uri);
...
});
你的问题。 getDownloadUrl()
返回类似于https://firebasestorage.googleapis.com/...
的内容,而getFile()
的参数则需要本地文件系统可以解析的内容file:///tmp/...
。
contentResolver无法解析HTTPS URL,因为它无法写入URL(它不是文件系统可以访问的文件)。
真的,您只想使用reference.getFile(/* appropriate local file */)
,而不是获取下载网址,或通过getBytes()
或getStream()
在内存中下载;