当前位置: 首页 > 知识库问答 >
问题:

尝试将映像从firebase数据库获取到存储时出现存储异常

公孙英飙
2023-03-14

我正在尝试将一个图像存储到FireBase数据库中,我很确定所有的代码都可以很好地获取imagelink,因为它不再像以前那样显示错误。然而现在,当我上传图片时,出现了一个新问题。这与存储异常有关,我猜它在实际从存储中提取imagelink并将其插入数据库时遇到了问题。下面是我认为出现问题的代码:

private StorageReference PostsImagesReference;
private DatabaseReference PostsRef;
private FirebaseAuth mAuth;

private String Description;
private String saveCurrentDate;
private String saveCurrentTime;
private String postRandomName;
private String currentUserID;
//private String downloadUrl;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    mAuth = FirebaseAuth.getInstance();
    currentUserID = mAuth.getCurrentUser().getUid();

    PostsImagesReference = FirebaseStorage.getInstance().getReference();

    PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");

如果需要,这是我的整个代码:

 private Toolbar mToolbar;
private ProgressDialog loadingBar;

private ImageButton SelectPostImage;
private Button AddPostButton;
private EditText PostDescription;

private static final int Gallery_Pick = 1;
private Uri ImageUri;


private StorageReference PostsImagesReference;
private DatabaseReference PostsRef;
private FirebaseAuth mAuth;

private String Description;
private String saveCurrentDate;
private String saveCurrentTime;
private String postRandomName;
private String currentUserID;
//private String downloadUrl;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    mAuth = FirebaseAuth.getInstance();
    currentUserID = mAuth.getCurrentUser().getUid();

    PostsImagesReference = FirebaseStorage.getInstance().getReference();

    PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");

    SelectPostImage = (ImageButton) findViewById(R.id.select_post_image);
    AddPostButton = (Button) findViewById(R.id.add_post);
    PostDescription = (EditText) findViewById(R.id.post_description);
    loadingBar = new ProgressDialog(this);


    mToolbar = (Toolbar) findViewById(R.id.update_post_page_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("Update Post");


    SelectPostImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            OpenGallery();

        }
    });

    AddPostButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            ValidatePostInfo();
        }
    });

}

private void ValidatePostInfo()
{
    Description = PostDescription.getText().toString();

    if(ImageUri == null)
    {
        Toast.makeText(this, "Please select image", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(Description))
    {
        Toast.makeText(this, "Please say something about your image", Toast.LENGTH_SHORT).show();
    }
    else
    {
        loadingBar.setTitle("Post Uploading");
        loadingBar.setMessage("Please Wait");
        loadingBar.show();
        loadingBar.setCanceledOnTouchOutside(true);

        StoringImageToFirebaseStorage();

    }
}

private void StoringTextToFirebaseStorage(Uri downloadUrl)
{

    Calendar callForDate = Calendar.getInstance();
    SimpleDateFormat currentDate = new SimpleDateFormat("dd-MMMM-yyyy");
    saveCurrentDate = currentDate.format(callForDate.getTime());

    Calendar callForTime = Calendar.getInstance();
    SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss");
    saveCurrentTime  = currentTime.format(callForDate.getTime());

    postRandomName = saveCurrentDate + saveCurrentTime;


    HashMap postsMap = new HashMap();
    postsMap.put("uid", currentUserID);
    postsMap.put("date", saveCurrentDate);
    postsMap.put("time", saveCurrentTime);
    postsMap.put("description", Description);
    postsMap.put("image", downloadUrl.toString());

    PostsRef.child(currentUserID + postRandomName).updateChildren(postsMap)
            .addOnCompleteListener(new OnCompleteListener()
            {
                @Override
                public void onComplete(@NonNull Task task)
                {
                    if (task.isSuccessful())
                    {

                        SendUserToMainActivity();
                        Toast.makeText(PostActivity.this, "Post Uploaded", Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                    else
                    {
                        String message = task.getException().getMessage();
                        Toast.makeText(PostActivity.this, "Error Occurred: " + message, Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                }

            });


}


private void StoringImageToFirebaseStorage()
{
    Calendar callForDate = Calendar.getInstance();
    SimpleDateFormat currentDate = new SimpleDateFormat("dd-MMMM-yyyy");
    saveCurrentDate = currentDate.format(callForDate.getTime());

    Calendar callForTime = Calendar.getInstance();
    SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss");
    saveCurrentTime  = currentTime.format(callForTime.getTime());

    postRandomName = saveCurrentDate + saveCurrentTime;


    final StorageReference filePath = PostsImagesReference.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg");

    filePath.putFile(ImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
        {
            if (task.isSuccessful())
            {
                SendUserToMainActivity();

                PostsImagesReference.child("Post Images").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
                {
                    @Override
                    public void onSuccess(Uri uri)
                    {
                        StoringTextToFirebaseStorage(uri);


                    }
                }).addOnFailureListener(new OnFailureListener()
                {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Handle any errors
                    }
                });

                Toast.makeText(PostActivity.this, "Post Uploaded", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();


            }
            else
            {
                String message = task.getException().getMessage();
                Toast.makeText(PostActivity.this, "Error Occurred: " + message, Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();
            }
        }
    });
}




private void OpenGallery()
{
    Intent galleryIntent = new Intent();
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(galleryIntent, Gallery_Pick);
}






@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
    {
        ImageUri = data.getData();
        SelectPostImage.setImageURI(ImageUri);
    }
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
{
    int id = item.getItemId();

    if(id == android.R.id.home)
    {
        SendUserToMainActivity();
    }

    return super.onOptionsItemSelected(item);
}



private void SendUserToMainActivity()
{
    Intent mainIntent = new Intent(PostActivity.this, MainActivity.class);
    startActivity(mainIntent);
}

}

错误消息:

2020-06-02 13:54:22.594 3466-5776/com.example.app E/StorageException: StorageException has occurred.
Object does not exist at location.
 Code: -13010 HttpResult: 404

2020-06-02 13:54:22.594 3466-5776/com.example。app E/StorageException:{“错误”:{“代码”:404,“消息”:“找不到。无法获取对象”,“状态”:“get_object”}}java.io。IOException:{“error”:{“code”:404,“message”:“Not Found.Could Not get object”,“status”:“get_object”}},位于com.google.firebase.storage.network.NetworkRequest.parseResponse(com.gooleg.firebase:firebase-storage@@17.0.0:455),位于位于com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.gooleg.firebase:firebase-storage@@17.0.0:426com.google.firebase.storage.internal.IxponentialBackoffSender.sendWithExponentialBackup(com.goole.firebasce:firebase-storage@@17.0.0:70)位于com.googel.firebase.rtorage.intral.ExponentialBackoffSender.sendWIthExponentionalBackoff(com.gougle.firefase:firebasure-storage@17.0.0:62java.lang.Thread.run(Thread.java:919)2020-06-02 13:54:22.602 3466-3466/com.example上java.util.concurrent.ThreadPoolExecutor$Worker.run处的java.util.concurrent。ThreadPool执行器.runWorker(ThreadPoolExecutor java:1167)。app E/RecyclerView:未连接布局管理器;跳过布局

共有1个答案

萧胜
2023-03-14

您正在尝试获取``文件夹的下载URL,这是不可能的:

PostsImagesReference.child("Post Images").getDownloadUrl().addOnSuccessListener(...

您需要在上调用putFile的StorageReference上调用getDownloadUrl()

filePath.getDownloadUrl().addOnSuccessListener(...

 类似资料:
  • 所以,我想让用户同时上传多个图像到firebase存储。我已经成功地完成了这部分,但现在我想要取回图像。一开始,我上传一个图像给每个用户,并将图像id保存到实时数据库中,这样就很容易通过图像id检索回图像。但是现在我如何为每个用户保存多个图像到实时数据库我不能使用相同的子名,因为它将被替换为旧的。有什么想法吗?? 上载图像并在实时数据库上设置id: 我的firebase数据库:

  • 在这个应用程序中,我点击一张照片,要么是通过相机,要么是从图库上传,然后把它传递给一个提供商。我想将该映像上传到Firebase存储区,然后将其映像Url与其他字符串和double类型信息一起存储在一个实时数据库中。但我似乎不能这样做,因为数据似乎在图像url可以被处理之前就上传到了实时数据库中。图像仍然正确地上载在存储中。下面是我的Provider类的代码。 有关的方法有uploadPic()和

  • 问题内容: 我想将图像的下载URL放入Firebase数据库中。我可以将图像上传到存储中,但无法弄清楚如何将URL与其余的“帖子”一起放入数据库。 问题答案: 像这样组织您和func: 接下来,只需连接并保存到您的节点即可。 您也可以查看我有关上传数据并将URL保存到数据库的答案 希望能帮助到你

  • 下面是我得到的控制台输出和错误: 下面是我按下Get Images按钮后的控制台输出,您可以看到图像的url为: