我正在尝试上传一个PDF文件到Firebase存储。我已经参考了官方文档(* click here *)并修改了代码以满足我的需要。
"我想达到的目的"
1.名为“mypdf.pdf”的文件位于“/storage/emulated/0/downloads”(基本上是公共下载目录)
1.把那个文件上传到存储器。
"我试过了“
我尝试使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/mypdf.pdf"
访问该文件,然后将其转换为URI并将其提供给firebase putFile()
函数。但是,它给了我一个PERMISSION DENIED
的异常,尽管我已经在manifest中提到了权限并在运行时请求权限。应用程序也由用户授予权限。
在onCreate()
内部调用
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/mypdf.pdf");
if (file.exists()) {
uploadFileFromFirebaseTut();
Log.d("FileLog", "onCreate: found the file");
} else Log.d("MainActivity.this", "onCreate: could not find file");
} else
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 81);
上传文件的函数
private void uploadFileFromFirebaseTut() {
StorageReference storageRef = storage.getReference();
UploadTask uploadTask;
Uri file = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/mypdf.pdf"));
StorageReference riversRef = storageRef.child("uploads/mypdf.pdf");
uploadTask = riversRef.putFile(file);
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.d("File Upload failed", "onFailure: "+exception);
Toast.makeText(MainActivity.this, "File Upload Failed...", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
// ...
Toast.makeText(MainActivity.this, "File Uploaded..", Toast.LENGTH_SHORT).show();
}
});
}
日志
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
StorageException E /storage/emulated/0/Download/mypdf.pdf: open failed: EACCES (Permission denied)
java.io.FileNotFoundException: /storage/emulated/0/Download/mypdf.pdf: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at java.io.FileInputStream.<init>(FileInputStream.java:115)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1484)
at com.google.firebase.storage.UploadTask.<init>(UploadTask.java:152)
at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:241)
at com.example.firebaseupload.MainActivity.uploadFileFromFirebaseTut(MainActivity.java:84)
at com.example.firebaseupload.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:8153)
at android.app.Activity.performCreate(Activity.java:8137)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3698)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:263)
at android.app.ActivityThread.main(ActivityThread.java:8296)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8178)
at libcore.io.IoBridge.open(IoBridge.java:478)
at java.io.FileInputStream.<init>(FileInputStream.java:160)
at java.io.FileInputStream.<init>(FileInputStream.java:115)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1484)
at com.google.firebase.storage.UploadTask.<init>(UploadTask.java:152)
at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:241)
at com.example.firebaseupload.MainActivity.uploadFileFromFirebaseTut(MainActivity.java:84)
at com.example.firebaseupload.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:8153)
at android.app.Activity.performCreate(Activity.java:8137)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3698)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:263)
at android.app.ActivityThread.main(ActivityThread.java:8296)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
StorageException E StorageException has occurred.
清单包含这些内容
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
1条答案
按热度按时间bihw5rsg1#
这是有点棘手的理解为什么文件没有得到上传。但作为@SambhavKhandelwal建议有一个额外的权限检查,这是有道理的,有一个权限问题。
所以,我没有找到确切的解决方案,但,我然后保存到应用程序特定的媒体目录文件,它开始工作