Imagens não carregadas do URL por Picasso do Firebase Database
Model class
public class Category
{
private String Name;
private String Image;
public Category(String name, String image) {
Name = name;
Image = image;
}
public Category() {
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
}
Classe de atividade
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference reference;
FirebaseStorage storage;
StorageReference storageReference;
//Add new menu
MaterialEditText edtTxtName;
Button selectImage;
Button uploadImage;
//Adding new category
Category newCategory;
Uri savedImageUri;
private final int PICK_IMAGE_REQUEST=71;
MaterialEditText edtTxtNewCategoryName;
FloatingActionButton fab;
FirebaseRecyclerAdapter<Category,MenuViewHolder> recyclerAdapter;
TextView userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
edtTxtNewCategoryName=findViewById(R.id.edt_txt_new_item_name);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Menu Mangement");
setSupportActionBar(toolbar);
fab =findViewById(R.id.fab);
//Firebase init
database=FirebaseDatabase.getInstance();
reference=database.getReference("Category");
storage=FirebaseStorage.getInstance();
storageReference=storage.getReference();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDailog();
}
});
DrawerLayout drawer =findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Setting header name
/* View view=navigationView.getHeaderView(0);
userName = view.findViewById(R.id.username);
userName.setText(Common.currentUser.getName());*/
//View init
recyclerView=findViewById(R.id.recycler_menu);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
loadMenu();
}
private void selectImage() {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode==PICK_IMAGE_REQUEST && resultCode==Activity.RESULT_OK
&& data!=null && data.getData()!=null)
{
savedImageUri=data.getData();//getting uri
selectImage.setText("Image Selected !");
}
}
private void uploadImage() {
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setMessage("Uploading Image");
progressDialog.show();
String image= UUID.randomUUID().toString();
final StorageReference imageFolder=storageReference.child("images/"+image);
imageFolder.putFile(savedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(Home.this, "Uploaded", Toast.LENGTH_SHORT).show();
imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
@Override
public void onSuccess(Uri uri)
{
newCategory=new Category(edtTxtName.getText().toString(),uri.toString());
Toast.makeText(Home.this, ""+uri.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(Home.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress=(100.0 * taskSnapshot.getBytesTransferred()
/ taskSnapshot.getTotalByteCount());
progressDialog.setMessage("Uploaded "+progress+" %");
}
});
}
private void showDailog() {
final AlertDialog.Builder alertDailog=new AlertDialog.Builder(this);
alertDailog.setTitle("Add new Category");
alertDailog.setMessage("Please fill all the fields");
LayoutInflater inflater=this.getLayoutInflater();
View view=inflater.inflate(R.layout.add_new_menu_layout,null);
edtTxtName=view.findViewById(R.id.edt_txt_new_item_name);
alertDailog.setView(view);
alertDailog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alertDailog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(newCategory!=null){
reference.push().setValue(newCategory);
}
}
});
alertDailog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDailog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDailog.show();
}
private void loadMenu()
{
recyclerAdapter=new FirebaseRecyclerAdapter<Category, MenuViewHolder>(
Category.class,R.layout.menu_layout,
MenuViewHolder.class,reference) {
@Override
protected void populateViewHolder(MenuViewHolder viewHolder, final Category model, int position)
{
viewHolder.menuName.setText(model.getName());
Picasso.get().load(model.getImage()).into(viewHolder.imageView);
viewHolder.setItemClickListner(new ItemClickListner() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//Getting menuId
Intent intent=new Intent(Home.this,FoodList.class);
intent.putExtra("CategoryId",recyclerAdapter.getRef(position).getKey());
startActivity(intent);
}
});
}
};
recyclerAdapter.notifyDataSetChanged();//notifiy us if data has been changed.
recyclerView.setAdapter(recyclerAdapter);
}
Na classe de atividade, o adaptador firebase é implementado. Por referência ao banco de dados firebase, as imagens são carregadas apenas com o ID "-LO061DOhjG2hVZlqY79", mas com IDs como '01' '02' não estão carregando
Adapter está carregando imagens muito lentamente Saída deste código
Ajuda será muito apreciada