Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple images from gallery

Tags:

android

I am developing an app to add multiple images into an ImageView. Now I can select Images from google Photos app but not from gallery. I am getting an Exception something like :- java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20170708_133105.jpg: open failed: EACCES (Permission denied) . Can anyone suggest me what I am missing in my code

Here is my Activity

public class MainActivity extends AppCompatActivity {
      Button camera, galary;
      ImageView imageView;
      private static final int CAMERA_PIC_REQUEST = 1337;
      private static final int SELECT_IMAGE = 1338;
      private Bitmap bitmap;
      ArrayList<Uri> imagesUriArrayList ;
      ListView listView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    camera = (Button) findViewById(R.id.button_camera);
    galary = (Button) findViewById(R.id.button_galary);
    imageView = (ImageView) findViewById(R.id.image);

    listView = (ListView) findViewById(R.id.listView);
    listView.setVisibility(View.INVISIBLE);

    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    });
    galary.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

            startActivityForResult(intent, SELECT_IMAGE);
        }
    });



}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && null != data) {
        bitmap = (Bitmap) data.getExtras().get("data");

        imageView.setImageBitmap(bitmap);
    }
    if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
        imagesUriArrayList =  new ArrayList();
        for (int i = 0; i < data.getClipData().getItemCount(); i++) {
            imagesUriArrayList.add(data.getClipData().getItemAt(i).getUri());
        }
        Log.e("SIZE", imagesUriArrayList.size() + "");
        listView.setVisibility(View.VISIBLE);


        DataAdapter adapter = new DataAdapter(MainActivity.this,MainActivity.this, imagesUriArrayList);
        listView.setAdapter(adapter);

        }

     else if (resultCode == this.RESULT_CANCELED) {
        Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
    }
}}

Here is my adapter class

public class DataAdapter extends ArrayAdapter{
     ArrayList<Uri> imagesUriArrayList;
     Activity mainActivity;
     ImageView imageview;
     Bitmap bitmap = null;
     Context context;
public DataAdapter(Context context, Activity mainActivity, ArrayList imagesUriArrayList) {
    super(context,R.layout.listview,imagesUriArrayList);
    this.imagesUriArrayList = imagesUriArrayList;
    this.mainActivity = mainActivity;
    this.context = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview, parent, false);
        imageview = (ImageView) convertView.findViewById(R.id.imageView);
    }

    Uri selectedImage = imagesUriArrayList.get(position);

    try {
        bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),selectedImage);
    } catch (IOException e) {
        e.printStackTrace();
    }

    imageview.setImageBitmap(bitmap);



    return  convertView;
}}
like image 637
Dubey.A Avatar asked Jan 30 '23 01:01

Dubey.A


1 Answers

try this

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select Picture"), 5);

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {

        if (requestCode == PICK_IMAGE_MULTIPLE) {

            Log.e("++data", "" + data.getClipData().getItemCount());// Get count of image here.

            Log.e("++count", "" + data.getClipData().getItemCount());

            if (data.getClipData().getItemCount() > 5) {
                adapter.notifyDataSetChanged();
                Snackbar snackbar = Snackbar
                        .make(findViewById(R.id.btnSelectImg), "You can not select more than 15 images", Snackbar.LENGTH_LONG)
                        .setAction("RETRY", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(Intent.createChooser(intent, "Select Picture"), 5);
                            }
                        });
                snackbar.setActionTextColor(Color.BLUE);
                View sbView = snackbar.getView();
                TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                textView.setTextColor(Color.RED);
                snackbar.show();

            } else {
                imagesUriArrayList.clear();

                for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                    imagesUriArrayList.add(data.getClipData().getItemAt(i).getUri());
                }
                Log.e("SIZE", imagesUriArrayList.size() + "");
                adapter = new DataAdapter(MainActivity.this, imagesUriArrayList);
                imageresultRecycletview.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

        }
    }

}

Load image like this

Bitmap bitmap=BitmapFactory.decodeFile(imagesUriArrayList.get(position));
bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
resultImageview.setImageBitmap(bitmap);

ask me in case of any query

like image 161
AskNilesh Avatar answered Feb 03 '23 08:02

AskNilesh