android firebase上传文件中的位置不存在对象

时间:2019-07-08 07:31:50

标签: java android firebase firebase-storage

我正在尝试使用Firebase存储服务创建一个应用程序。我有一个功能,可以从设备本地存储中保存图像,使用图片的URI将其保存到Firebase。尝试将图像上传到Firebase服务器时,出现“对象在该位置不存在”的问题,可能是我的URI出现问题。

我正在添加整个活动,以便您可以查看发生的情况-

UserControl

我错误地实现了URI吗?

编辑-

尝试通过注释解决此问题,但仍然遇到相同的错误-

Frame.Source

4 个答案:

答案 0 :(得分:1)

对我来说,此代码可以上传。我是在评论部分的https://www.youtube.com/watch?v=lPfQN-Sfnjw&list=PLrnPJCHvNZuBf5KH4XXOthtgo6E4Epjl8&index=4处找到的。还将Firebase存储中的身份验证设置为true

private void uploadFile(){
    if(mImageUri!=null){
            storageReference.putFile(mImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>()
            {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
                {
                    if (!task.isSuccessful())
                    {
                        throw task.getException();
                    }
                    return storageReference.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>()
            {
                @Override
                public void onComplete(@NonNull Task<Uri> task)
                {
                    if (task.isSuccessful())
                    {
                        Uri downloadUri = task.getResult();
                        Log.e("TAG", "then: " + downloadUri.toString());


                        Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
                                downloadUri.toString());
                        databaseReference.push().setValue(upload);
                        Toast.makeText(MainActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    } else
                    {
                        Toast.makeText(MainActivity.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
    }
    else{
        Toast.makeText(this, "Please upload image!", Toast.LENGTH_SHORT).show();
    }
}

答案 1 :(得分:0)

尝试使用此代码

StorageReference imageReference = storageReference.child("uploads");  

              UploadTask uploadTask = imageReference.putFile(imageuri);
                    uploadTask.addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(verify.this, "Upload failed!", Toast.LENGTH_SHORT).show();
                        }
                    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            if (taskSnapshot.getMetadata() != null) {
                                if (taskSnapshot.getMetadata().getReference() != null) {

                                    Task<Uri> result = taskSnapshot.getStorage().getDownloadUrl();
                                    result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                                        @Override
                                        public void onSuccess(Uri uri) {

答案 2 :(得分:0)

替换以下代码:

            StorageReference fileRefrence = storageReference.child(System.currentTimeMillis()
                    + "." + getFileExtension(imageUri));

与此:

            StorageReference fileRefrence = storageReference.child(imageUri.getLastPathSegment());

答案 3 :(得分:0)

这是我用于项目及其工作的代码... 还要确保

  • 您已在Firebase存储上公开了规则
  • 在清单文件中添加了访问电话存储和访问互联网的权限
  • 添加了Firebase存储访问所需的所有依赖项

StorageReference storageReference;
storageReference = FirebaseStorage.getInstance()。getReference();

StorageReference refStorage = storageReference.child(“ images /” + uri.getLastPathSegment());    UploadTask uploadTask = refStorage.putFile(uri);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw new Exception("task is not succeeded");
        } return refStorage.getDownloadUrl();
    }//end of throws Exception method
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            downUri = task.getResult();
        } else {
            // Handle failures
            // ...
            Log.d("<<<<<<<<", "<<<<<<<<<<<<<<<<<<<< not uplooaded ");
        }
    }//end of onComplete()for getting download uri
});