无法从Firebase存储中检索图像 [英] Cannot retrieve images from firebase storage

查看:98
本文介绍了无法从Firebase存储中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经成功地将图像上传到了Firebase,但是在将其检索到回收者视图时,我遇到了问题,即根本无法检索图像. 看一下我的源代码:

So Far i have succeeded uploading images to firebase but while retrieving them to recycler view i'm facing problems i.e images are not being retrieved at all. Take a look at my source code:

这是Recycler View适配器:

This is Recycler View Adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ImageViewHolder> {
Context mContext;
private List<Upload> mUploads;

public MyAdapter(Context context, List<Upload> uploads) {
    this.mContext = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.layout_images, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getName());
    Picasso.get()
            .load(uploadCurrent.getImageUrl())
            .fit()
            .centerCrop()
            .into(holder.imageView);
}

@Override
public int getItemCount() {
    return mUploads.size();
}

public class ImageViewHolder extends RecyclerView.ViewHolder {
    public TextView textViewName;
    public ImageView imageView;

    public ImageViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);
    }
}

这是我检索图像的主要班级:

This is my main class to retrieve images:

public class Viewimages extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;



private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;

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

    mRecyclerView = findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


    mUploads = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                mUploads.add(upload);
            }

            mAdapter = new MyAdapter(Viewimages.this, mUploads);

            mRecyclerView.setAdapter(mAdapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(Viewimages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });
}

在此图像中,图像名称已成功获取,但未填充Cardview图像内部

这是存储url的数据库图片

推荐答案

正像@Alex所说的那样,您没有存储有效的url,这就是为什么您无法从数据库中检索那些图像.

Exactly as @Alex said, you're not storing the valid url, that's why you're not able to retrieve those images from the database.

为了将URL从Firebase存储完美地存储到Firebase数据库,可以使用如下代码:

For storing the url perfectly from your Firebase storage to your Firebase Database, you can use a code like this:

此代码还包含可将图像上传到Firebase存储的部分,因此我认为这将使您与代码相关,甚至可以为您提供更多帮助.

This code also contains the part where you can upload the image to your firebase storage, so I think this would make you relate to your code and may help you, even more.

private void uploadFile(Bitmap bitmap) {

        FirebaseStorage storage = FirebaseStorage.getInstance();
        final StorageReference storageRef = storage.getReference();

        final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
        byte[] data = baos.toByteArray();
        final UploadTask uploadTask = ImagesRef.putBytes(data);



        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.i("whatTheFuck:",exception.toString());
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.

                Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                    @Override
                    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if (!task.isSuccessful()) {
                            Log.i("problem", task.getException().toString());
                        }

                        return ImagesRef.getDownloadUrl(); 
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                            Uri downloadUri = task.getResult();

                            DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());

                            Log.i("seeThisUri", downloadUri.toString());// This is the one you should store

                            ref.child("imageURL").setValue(downloadUri.toString());


                        } else {
                            Log.i("wentWrong","downloadUri failure");
                        }
                    }
                });
             }
        });

    }

您可以在downloadUri.toString()的代码中看到的url,这是您应该存储在数据库中的URL.

The url that you can see in the code from downloadUri.toString(), this is the one you should be storing in your database.

这篇关于无法从Firebase存储中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆