1 year ago

#386135

test-img

Fanuel

Android Display multiple image selected from gallery to imageview

I'm trying to display multiple images I selected from a gallery into an imageView or RecyclerView. I'm using this repository I found on github.

Here is the Java code:

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.zfdang.multiple_images_selector.ImagesSelectorActivity;
import com.zfdang.multiple_images_selector.SelectorSettings;
import java.util.ArrayList;

public class DoodleActivity extends AppCompatActivity {
    TextView textView;
    Button pick;
    ImageView img1, img2, img3;
    ArrayList<Uri> uri = new ArrayList<>();
    private static final int REQUEST_CODE = 123;
    private ArrayList<String> mResults = new ArrayList<>();
    ArrayList<Uri> ImageList = new ArrayList<Uri>();
    private Uri image_uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doodle);
        Fresco.initialize(getApplicationContext());

        textView = findViewById(R.id.total);
        recyclerView = findViewById(R.id.recyclerView);
        pick = findViewById(R.id.link);
        img1 = findViewById(R.id.image1);
        img2 = findViewById(R.id.image2);
        img3 = findViewById(R.id.image3);

        if (ContextCompat.checkSelfPermission(DoodleActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(DoodleActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},Read_Permission);
        }

        pick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // start multiple photos selector
                Intent intent = new Intent(DoodleActivity.this, ImagesSelectorActivity.class);
                intent.putExtra(SelectorSettings.SELECTOR_MAX_IMAGE_NUMBER, 5);

                intent.putExtra(SelectorSettings.SELECTOR_MIN_IMAGE_SIZE, 100000);
                intent.putExtra(SelectorSettings.SELECTOR_SHOW_CAMERA, true);
                intent.putStringArrayListExtra(SelectorSettings.SELECTOR_INITIAL_SELECTED_LIST, mResults);
                startActivityForResult(intent, REQUEST_CODE);
                intent.setAction(Intent.ACTION_GET_CONTENT);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // get selected images from selector
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                assert mResults != null;
                ClipData clipData = data.getClipData();

                if (clipData !=null){
                    int countClipData = data.getClipData().getItemCount();

                    img1.setImageURI(clipData.getItemAt(0).getUri());
                    img2.setImageURI(clipData.getItemAt(1).getUri());
                    img3.setImageURI(clipData.getItemAt(2).getUri());

                    int currentImageSelect = 0;
                    while (currentImageSelect < countClipData){
                        image_uri = data.getClipData().getItemAt(currentImageSelect).getUri();

                        ImageList.add(image_uri);
                        currentImageSelect = currentImageSelect +1;
                    }
                }

                // show results in textview
                StringBuffer sb = new StringBuffer();
                sb.append(String.format("Totally %d images selected:", mResults.size())).append("\n");
                for (String result : mResults) {
                    sb.append(result).append("\n");
                }
                textView.setText(sb.toString());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

java

android

android-recyclerview

android-imageview

0 Answers

Your Answer

Accepted video resources