我正在创建一个活动,用户可以从手机照片库中选择图像,然后这些照片将使用毕加索显示在“回收者视图”中并存储在Firebase存储器中。我已经尝试了所有解决方案,但是图像未在Recycler View中加载。
我的AddProduct.java代码
imgAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);//Specify the type of intent
intent.setAction(Intent.ACTION_GET_CONTENT); //What action needs to be performed.
startActivityForResult(Intent.createChooser(intent, "Select Product Image"), PICK_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE && resultCode == RESULT_OK){
if(data.getClipData()!=
int totalItemsSelected = data.getClipData().getItemCount();
for(int j = 0; j < totalItemsSelected; j++){
Uri fileUri = data.getClipData().getItemAt(j).getUri();
mImageUrls.add(fileUri);
}
}else if(data.getData()!=null){
Toast.makeText(getApplicationContext(), "Single file selected.", Toast.LENGTH_SHORT).show();
}
}
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.rvPhotos);
recyclerView.setLayoutManager(layoutManager);
AddProductPicAdapter adapter = new AddProductPicAdapter(this, mImageUrls);
recyclerView.setAdapter(adapter);
}
我的适配器代码
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class AddProductPicAdapter extends RecyclerView.Adapter<AddProductPicAdapter.ViewHolder> {
private ArrayList<Uri> mImageUrls;
private Context mContext;
public AddProductPicAdapter(Context context, ArrayList<Uri> imageUrls) {
this.mImageUrls = imageUrls;
this.mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { //This binds the layout file.
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.add_product_pic_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { //This binds the data.
Picasso.get().load(mImageUrls.get(i)).fit().centerCrop().into(viewHolder.image);
}
@Override
public int getItemCount() {
return mImageUrls.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.ivProdImage);
}
}
}