我正在尝试制作一个可以将图像上传到Firebase并进行检索的android应用。上传进行得很顺利,但是我无法检索图像。我能够检索附在图像上的文本,但无法检索下载图像本身。我正在使用下滑库从Firebase获取图像,然后将其显示在回收者视图中。
编辑
Question referred在这里,最上面的答案告诉您将addOnSuccessListener
添加到storageReference。但是我已经在ImageSelect.java类中完成了
RecyclerViewAdapter.java的代码
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<ImageUploadInfo> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<ImageUploadInfo> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);
holder.imageNameTextView.setText(UploadInfo.getImageName());
//Loading image from Glide library.
Glide.with(context).load(UploadInfo.getImageURL()).into(holder.imageView);
}
@Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView imageNameTextView;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageNameTextView = (TextView) itemView.findViewById(R.id.ImageNameTextView);
}
}
}
我得到的输出是
ImageSelect.java类的代码
请帮助我解决我的问题。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_select);
// Assign FirebaseStorage instance to storageReference.
storageReference = FirebaseStorage.getInstance().getReference();
// Assign FirebaseDatabase instance with root database name.
databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);
// Assigning Id to ProgressDialog.
progressDialog = new ProgressDialog(ImageSelect.this);
clickedImage = (ImageView) findViewById(R.id.imageView);
uploadButton = (Button) findViewById(R.id.upload);
caption = (EditText) findViewById(R.id.caption);
slidr = Slidr.attach(this);
if(CameraActivity.i == "1" )
{
//Bundle bundle = getIntent().getExtras();
// byte[] byteArray = getIntent().getByteArrayExtra("bytearray");
// bitmap2 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//String setter = bundle.getString("setter");
imageUri = getIntent().getData();
clickedImage.setImageURI(imageUri);
}
else {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(photoPath, options);
File f = new File(photoPath);
imageUri = getIntent().getData();
clickedImage.setImageURI(imageUri);
}
uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UploadImageFileToFirebaseStorage();
}
});
}
// Creating Method to get the selected image file Extension from File Path URI.
public String GetFileExtension(Uri uri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
// Returning the file Extension.
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri)) ;
}
// Creating UploadImageFileToFirebaseStorage method to upload image on storage.
public void UploadImageFileToFirebaseStorage(){
// Setting progressDialog Title.
progressDialog.setTitle("Image is Uploading...");
// Showing progressDialog.
progressDialog.show();
// Creating second StorageReference.
StorageReference storageReference2nd = storageReference.child(Storage_Path + System.currentTimeMillis() + "." +GetFileExtension(imageUri));
// Adding addOnSuccessListener to second StorageReference.
storageReference2nd.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Getting image name from EditText and store into string variable.
String TempImageName = caption.getText().toString().trim();
// Hiding the progressDialog after done uploading.
progressDialog.dismiss();
// Showing toast message after done uploading.
Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
@SuppressWarnings("VisibleForTests")
ImageUploadInfo imageUploadInfo = new ImageUploadInfo(TempImageName, taskSnapshot.getMetadata().getReference().getDownloadUrl().toString().toString());
// Getting image upload ID.
String ImageUploadId = databaseReference.push().getKey();
// Adding image upload id s child element into databaseReference.
databaseReference.child(ImageUploadId).setValue(imageUploadInfo);
}
})
// If something goes wrong .
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Hiding the progressDialog.
progressDialog.dismiss();
// Showing exception erro message.
Toast.makeText(ImageSelect.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
})
// On progress change upload time.
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
// Setting progressDialog Title.
progressDialog.setTitle("Image is Uploading...");
}
});
}
}