无法从 firebase 存储中检索图像
Cannot retrieve images from firebase storage
到目前为止,我已成功将图像上传到 firebase,但在将它们检索到回收站视图时我遇到了问题,即根本无法检索图像。
看看我的源代码:
这是 Recycler 视图适配器:
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);
}
}
这是我主要的class检索图片:
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();
}
});
}
In this image the image name is successfully fetched but inside the cardview images are not being populated
This the picture of database where url is stored
您没有得到任何东西,因为在您的实际数据库中,您没有存储正确的图像 url,而是存储内存中某些对象的一些地址。所以基本上问题是当您上传图像并将 url 保存到您的 Firebase 数据库时。所以你应该把那些uri对象转换成String,这样才能正确存储。
com.google.android.gms.tasks.zzu@13dd
不是有效的 url 地址。
正如@Alex 所说,您没有存储有效的 url,这就是您无法从数据库中检索这些图像的原因。
要将 url 从您的 Firebase 存储完美存储到您的 Firebase 数据库,您可以使用如下代码:
此代码还包含您可以将图像上传到您的 firebase 存储的部分,因此我认为这会让您与您的代码产生关联并可能对您有所帮助,甚至更多。
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,这是您应该存储在数据库中的那个。
到目前为止,我已成功将图像上传到 firebase,但在将它们检索到回收站视图时我遇到了问题,即根本无法检索图像。 看看我的源代码:
这是 Recycler 视图适配器:
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);
}
}
这是我主要的class检索图片:
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();
}
});
}
In this image the image name is successfully fetched but inside the cardview images are not being populated
This the picture of database where url is stored
您没有得到任何东西,因为在您的实际数据库中,您没有存储正确的图像 url,而是存储内存中某些对象的一些地址。所以基本上问题是当您上传图像并将 url 保存到您的 Firebase 数据库时。所以你应该把那些uri对象转换成String,这样才能正确存储。
com.google.android.gms.tasks.zzu@13dd
不是有效的 url 地址。
正如@Alex 所说,您没有存储有效的 url,这就是您无法从数据库中检索这些图像的原因。
要将 url 从您的 Firebase 存储完美存储到您的 Firebase 数据库,您可以使用如下代码:
此代码还包含您可以将图像上传到您的 firebase 存储的部分,因此我认为这会让您与您的代码产生关联并可能对您有所帮助,甚至更多。
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,这是您应该存储在数据库中的那个。