0

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.

Varun Sharma
  • 132
  • 2
  • 13
  • Do you have access to the URI in the adapter? – S-L-R Jun 15 '17 at 12:59
  • Because if you are sure that the URI are correct this should help : https://stackoverflow.com/a/4717740/8160570 – S-L-R Jun 15 '17 at 13:01
  • Yes. The textViewGrid is basically showing the Uri as /storage/.. – Varun Sharma Jun 15 '17 at 13:01
  • Basically I am getting a list of the paths of all the images in the adapter ArrayList photos – Varun Sharma Jun 15 '17 at 13:03
  • Try this : https://stackoverflow.com/questions/3879992/how-to-get-bitmap-from-an-uri/4717740#4717740 – S-L-R Jun 15 '17 at 13:03
  • public void onBindViewHolder(PhotoHolder holder, int position) { Uri photo = photos.get(position); Bitmap bitmap = null; try { bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),photo); } catch (IOException e) { e.printStackTrace(); } holder.imageViewGrid.setImageBitmap(null); holder.imageViewGrid.setImageBitmap(bitmap); holder.textViewGrid.setText(photo+""); } – Varun Sharma Jun 15 '17 at 13:15
  • Tried it but result is the same – Varun Sharma Jun 15 '17 at 13:15
  • /storage/emulated/0/Screenshots/Screenshot_20170613-115540.png. This is the content of Uri photo but bitmap is null. – Varun Sharma Jun 15 '17 at 13:18

1 Answers1

0

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

Meikiem
  • 1,876
  • 2
  • 11
  • 19
  • /storage/emulated/0/Screenshots/Screenshot_20170613-115540.png. This is the value of Uri photo – Varun Sharma Jun 15 '17 at 13:17
  • did you check your Uri for null and are you sure your Uri is correct? – Meikiem Jun 15 '17 at 13:18
  • Uri has the above mentioned value. And thats the correct uri for the image. It is generated when I select multiple images in the first activity. Moreover every element of ArrayList photos is different corresponding to the different multiple images – Varun Sharma Jun 15 '17 at 13:21
  • Corrections: The path is correct and I am not sure about the uri. I am having doubts where I added the uris in the list in onActivityResult in the first activity. It is generating the correct path though is that the correct way to deal with uris? – Varun Sharma Jun 15 '17 at 13:25
  • 1
    you shoud make Uri from your file path, like this:File file = .... Uri uri = Uri.fromFile(file); imageView.setImageURI(uri); – Meikiem Jun 15 '17 at 13:44
  • Same result as before.Only the text view is showing the path like '/storage/emulated/0/Screenshots/Image.png – Varun Sharma Jun 16 '17 at 05:05