如何在recyclerview onclick监听器中打开camara和gallery?

时间:2016-12-28 12:54:57

标签: android

ItemAdapter.java

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView textView;
        String item;
        private final Context context;
        public ViewHolder(final View itemView) {
            super(itemView);
            context = itemView.getContext();
            itemView.setOnClickListener(this);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(v.getId()==textView.getId()){
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        context.startActivity(intent);
                    }
                    else
                    {
                        Toast.makeText(context,"Hellooo",Toast.LENGTH_LONG).show();
                    }

                }
            });
            textView = (TextView) itemView.findViewById(R.id.textView);
        }

1 个答案:

答案 0 :(得分:0)

试试这个

imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                selectImage();
            }

        });
    }

 private void selectImage() { 
        final CharSequence[] items = {"Take Photo", "Choose from Library",
                "Cancel"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    activeTakePhoto();
                } else if (items[item].equals("Choose from Library")) {
                    activeGallery();
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }


    private void activeTakePhoto() {  //open camera
        String filename = "Pic_" + System.currentTimeMillis() + ".jpg";
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(Environment.getExternalStorageDirectory(), filename);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }

    private void activeGallery() { // open gallery
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }
  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                    imageUri = data.getData();
                    imageView.setImageURI(imageUri);
                }

                break;

            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    try {
                        selectedImage = imageUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        imageView.setImageURI(null);
                        imageView.setImageURI(imageUri);
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
        }
    }