将文件从我的应用程序上传到Firebase时出现问题

时间:2018-12-20 22:10:37

标签: java android firebase firebase-storage

嗨,我在将文件从我的应用程序上传到Firebase时遇到问题。我正在使用Android Studio firebase助手来创建它,但它无法正常工作,并且会从失败功能中向我打印文本。同样在Logcat,它的打印内容是:

发生未知错误,请检查HTTP结果代码和服务器响应的内部异常。      代码:-13000 Http结果:0 2018-12-20 23:07:23.680 30476-30564 / com.example.johny.test E / StorageException:/ storage / emulated / 0(是目录)     java.io.FileNotFoundException:/ storage / emulated / 0(是目录)

下面代码中的行riversRef.putFile(files)

谢谢您的时间

upload.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

                try {
                    File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath(),FILE_NAME);
                    nameoffile=FILE_NAME;
                    FileOutputStream fos=new FileOutputStream (file);
                    DataOutputStream dout = new DataOutputStream(fos);
                    //dout.writeInt(SignalData.size()); // Save line count
                    for(int i=0;i<SignalData.size();i++){
                        dout.writeChars(SignalData.get(i));
                    }
                    dout.close(); // ... and close.
                    //Toast.makeText(getActivity(),"File Successful Created",Toast.LENGTH_LONG).show();
                } catch (IOException exc) {
                    exc.printStackTrace();
                    Toast.makeText(getActivity(),"File NOT Created",Toast.LENGTH_LONG).show();
                }


                Uri files = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()));
                StorageReference riversRef = mStorageRef.child(nameoffile);

                riversRef.putFile(files)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                // Get a URL to the uploaded content
                                Toast.makeText(getActivity(),"File Upload Successfully",Toast.LENGTH_LONG).show();
                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                // Handle unsuccessful uploads
                                // ...
                                Toast.makeText(getActivity(),"File Didn't Upload",Toast.LENGTH_LONG).show();
                            }
                        });
                }
            }
        );
    }

1 个答案:

答案 0 :(得分:0)

  

E / StorageException:/ storage / emulated / 0 (是目录)   java.io.FileNotFoundException:/ storage / emulated / 0 (是   目录)

由于错误状态,您在此处引用的是目录,而不是文件

Uri files = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()));

检查您要查找的文件的路径并进行更改。

Firebase Storage文档中,您可以找到有关如何查找文件的示例。

您还可以指定文件的位置

Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));