我正在尝试显示Recyclerview
中的相机拍摄图像和画廊图像。它工作正常,但是当我捕获第二张图像或从图库中选择图像时,它将替换Recyclerview
中的第一张图像。我想显示我从相机中选择或捕获的Recyclerview
中的所有图像。
活动代码:
public void ShowDialog() {
final Dialog dialog = new Dialog(AttachDisputeDocActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.rc_img_dialog_layt);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.dimAmount = 0.5f;
dialog.getWindow().setAttributes(layoutParams);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
RelativeLayout UploadImage = dialog.findViewById(R.id.rela_upload);
RelativeLayout TakeCamera = dialog.findViewById(R.id.rela_camera);
UploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (ActivityCompat.checkSelfPermission(AttachDisputeDocActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AttachDisputeDocActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, DspConstant.REQUEST_GALLARY_IMAGE);
} else {
gallaryintent();
}
} catch (Exception e) {
e.printStackTrace();
}
dialog.dismiss();
}
});
dialog.show();
TakeCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (ActivityCompat.checkSelfPermission(AttachDisputeDocActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AttachDisputeDocActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, DspConstant.REQUEST_CAPTURE_IMAGE);
} else {
openCameraIntent();
}
} catch (Exception e) {
e.printStackTrace();
}
dialog.dismiss();
}
});
}
private void openCameraIntent() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".fileprovider", photoFile);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(pictureIntent, DspConstant.REQUEST_CAPTURE_IMAGE);
}
}
}
private void gallaryintent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent .setType("image/*");
startActivityForResult(galleryIntent, DspConstant.REQUEST_GALLARY_IMAGE);
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
imagefilepath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DspConstant.REQUEST_GALLARY_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
loadRecyclerView(picturePath);
} else if (requestCode == DspConstant.REQUEST_CAPTURE_IMAGE && requestCode == RESULT_OK && null != data) {
loadRecyclerView(imagefilepath);
}
}
private void loadRecyclerView(String image) {
documentlist.add(image);
attachDocAdapter = new AttachDocAdapter(this, documentlist);
recyclerView.setAdapter(attachDocAdapter);
attachDocAdapter.updateList(documentlist);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case DspConstant.REQUEST_GALLARY_IMAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
gallaryintent();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
case DspConstant.REQUEST_CAPTURE_IMAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCameraIntent();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
适配器代码:
public class AttachDocAdapter extends RecyclerView.Adapter<AttachDocAdapter.MyViewHolder> {
private ArrayList<String> imageslist=new ArrayList<>();
private Activity activity;
public AttachDocAdapter(Activity activity,ArrayList<String> imageslist)
{
this.activity=activity;
this.imageslist=imageslist;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView img_document;
CitiTextView txt_deleteimage;
public MyViewHolder(View itemView) {
super(itemView);
img_document=itemView.findViewById(R.id.img_document);
txt_deleteimage=itemView.findViewById(R.id.txt_delete);
}
}
@Override
public AttachDocAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemview= LayoutInflater.from(parent.getContext()).inflate(R.layout.rc_img_attach_layout,parent,false);
return new MyViewHolder(itemview);
}
@Override
public void onBindViewHolder(AttachDocAdapter.MyViewHolder holder, int position) {
Glide.with(activity).load(imageslist.get(position)).into(holder.img_document);
holder.txt_deleteimage.setOnClickListener(view -> removeItem(holder.getAdapterPosition()));
}
@Override
public int getItemCount() {
if (imageslist==null)
{
return 0;
}return imageslist.size();
}
public void updateList(ArrayList<String> imageslist) {
this.imageslist = imageslist;
notifyDataSetChanged();
}
public void removeItem(int position) {
imageslist.remove(position);
notifyItemRemoved(position);
}
}
当我单击TakeCamera时,三星S5没有任何反应。