问题
Okay the problem is that I am selecting multiple images and have to display all the selected images in another activity recycler view(Grid View). I got the Uri paths(Tried string as well) in the first activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
// When an Image is picked
if (requestCode == SELECT_PICTURE_MULTIPLE && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
String[] filePathColumn = { MediaStore.Images.Media.DATA };
imagesEncodedList = new ArrayList<Uri>();
if(data.getData()!=null){
Uri mImageUri=data.getData();
// Get the cursor
Cursor cursor = getContentResolver().query(mImageUri,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = Uri.parse(cursor.getString(columnIndex));
imagesEncodedList.add(imageEncoded);
cursor.close();
}else {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri);
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = Uri.parse(cursor.getString(columnIndex));
imagesEncodedList.add(imageEncoded);
cursor.close();
}
Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
}
}
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
super.onActivityResult(requestCode, resultCode, data);
}
And in the Second Activity I am getting the uris
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
images=new ArrayList<Uri>();
images=(ArrayList) getIntent().getSerializableExtra("IMAGES_LIST");
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerViewGrid);
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
recyclerViewAdapter = new RecyclerViewAdapter(images,this);
recyclerView.setAdapter(recyclerViewAdapter);
}
The RecyclerViewAdapter is:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.PhotoHolder> {
private Context context;
private ArrayList<Uri> photos;
public RecyclerViewAdapter(ArrayList<Uri> photos,Context context){
this.photos=photos;
this.context=context;
}
@Override
public PhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.photo_grid,parent,false);
PhotoHolder photoHolder = new PhotoHolder(view);
return photoHolder;
}
@Override
public void onBindViewHolder(PhotoHolder holder, int position) {
Uri photo = photos.get(position);
String path = photo.getPath();
File sd = Environment.getExternalStorageDirectory();
File image = new File(path);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
//bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
holder.imageViewGrid.setImageBitmap(bitmap);
holder.textViewGrid.setText(photo+"");
}
@Override
public int getItemCount() {
return this.photos.size();
}
public class PhotoHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView imageViewGrid;
public TextView textViewGrid;
public PhotoHolder(View itemView) {
super(itemView);
imageViewGrid = (ImageView)itemView.findViewById(R.id.imageViewGrid);
textViewGrid = (TextView)itemView.findViewById(R.id.textViewGrid);
}
@Override
public void onClick(View view) {
}
}
}
Now even though I have the path the image view is not getting the image. I have tried Picasso, Glide, and almost all the possible options I could conceive. Still have not found the error. Any help will be appreciated.
回答1:
You can do it By this code:
holder.imageViewGrid.setImageURI(null);
holder.imageViewGrid.setImageURI(photo);
just first set null to reset it.
let me know about the result
来源:https://stackoverflow.com/questions/44568115/set-an-imageview-in-a-viewholder-getting-uri-list-from-another-activity