我在 Android (SDK 34) 上使用 Firebase API 将文件从我的存储桶下载到我的共享下载文件夹。但是,当我调用
StorageReference.getFile(Uri uri)
时,创建0字节文件后失败。但是,如果我使用 getBytes
并自己将字节写入文件,则效果很好。
这是无法工作的代码:
// This does not. See the exception details below
public void download(String remotePath, Uri localUri) {
StorageReference fileRef = storageRef.child(remotePath);
fileRef
.getFile(localUri)
.addOnFailureListener(
error -> {
System.out.println(error.getMessage());
});
}
错误消息和堆栈跟踪:
error = {StorageException@26984} "com.google.firebase.storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response."
cause = {IOException@26987} "java.io.IOException: No such file or directory"
backtrace = null
cause = {IOException@26987} "java.io.IOException: No such file or directory"
backtrace = null
cause = {IOException@26987} "java.io.IOException: No such file or directory"
detailMessage = "No such file or directory"
stackTrace = {StackTraceElement[14]@26998}
0 = {StackTraceElement@27003} "java.io.UnixFileSystem.createFileExclusively0(Native Method)"
1 = {StackTraceElement@27004} "java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:349)"
2 = {StackTraceElement@27005} "java.io.File.createNewFile(File.java:1006)"
3 = {StackTraceElement@27006} "com.google.firebase.storage.FileDownloadTask.processResponse(FileDownloadTask.java:136)"
4 = {StackTraceElement@27007} "com.google.firebase.storage.FileDownloadTask.run(FileDownloadTask.java:228)"
5 = {StackTraceElement@27008} "com.google.firebase.storage.StorageTask.lambda$getRunnable$7$com-google-firebase-storage-StorageTask(StorageTask.java:1077)"
6 = {StackTraceElement@27009} "com.google.firebase.storage.StorageTask$$ExternalSyntheticLambda7.run(Unknown Source:2)"
7 = {StackTraceElement@27010} "com.google.firebase.concurrent.LimitedConcurrencyExecutor.lambda$decorate$0$com-google-firebase-concurrent-LimitedConcurrencyExecutor(LimitedConcurrencyExecutor.java:65)"
8 = {StackTraceElement@27011} "com.google.firebase.concurrent.LimitedConcurrencyExecutor$$ExternalSyntheticLambda0.run(Unknown Source:4)"
9 = {StackTraceElement@27012} "java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)"
10 = {StackTraceElement@27013} "java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)"
11 = {StackTraceElement@27014} "com.google.firebase.concurrent.CustomThreadFactory.lambda$newThread$0$com-google-firebase-concurrent-CustomThreadFactory(CustomThreadFactory.java:47)"
12 = {StackTraceElement@27015} "com.google.firebase.concurrent.CustomThreadFactory$$ExternalSyntheticLambda0.run(Unknown Source:4)"
13 = {StackTraceElement@27016} "java.lang.Thread.run(Thread.java:1012)"
但是,这段代码可以工作:
public void download(String remotePath, Uri localUri) {
StorageReference fileRef = storageRef.child(remotePath);
fileRef
.getBytes(1024 * 1024 * 16)
.addOnSuccessListener(
result -> {
try {
ContentResolver resolver = context.getContentResolver();
OutputStream stream = resolver.openOutputStream(localUri);
stream.write(result);
stream.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
});
}
这就是我创建localUri
的方式:
ContentResolver resolver = context.getContentResolver();
String fileName = UUID.randomUUID().toString().toUpperCase();
Uri imagesUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
Uri localUri = resolver.insert(imagesUri, values);
你知道这里出了什么问题吗?谢谢!
fileRef.getFile(uri) 尝试将远程文件从存储下载到传递给它的本地文件 URI。它必须是一个特定的文件路径,如文档所述(强调我的):
将此 StorageReference 处的对象异步下载到但是您将 URI 传递给 ContentProvider,而这不受支持。 SDK 不会尝试下载文件并将其放入 ContentProvider 中。 URI 必须标识一个文件。您可能更容易只传递 File 参数,并确保它指向应用程序可写入的位置,例如其指定的系统文件路径。
内部存储位置。