PS:这是之前提出的问题的更新。
我有一个自定义相机布局,我需要在单击时将图像设置为水平。我尝试使用recyclerview。 首先,单击的图像存储在名为 result 的位图中。
然后,我将图像的路径存储在名为 path 的变量中。 path变量通过bundle传递给RecycleViewAdapter。
然后,使用onBindViewHolder,我试图在水平视图中显示图像。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
Bitmap result =
BitmapFactory.decodeByteArray(picture, 0, picture.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), result, "Title", null);
horizontalList.add(path);
recycleViewAdapter = new RecycleViewAdapter(horizontalList);
horizontal_rv.setAdapter(recycleViewAdapter);
recycleViewAdapter.notifyDataSetChanged();
mBundle = new Bundle();
CameraActivity.mBundle.putString("abc", path);
horizontalList.add(path);
}
});
camera.captureImage();
}
});
然后传递给适配器类。
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {
Uri uri;
String path;
private List<Bitmap> horizontalList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView image_result;
public MyViewHolder(View view) {
super(view);
image_result = (ImageView) view.findViewById(R.id.image_result);
}
}
public RecycleViewAdapter(ArrayList<Bitmap> horizontalList) {
this.horizontalList = horizontalList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
path= CameraActivity.mBundle.getString("abc");
uri=Uri.parse(path);
holder.image_result.setImageURI(uri);
holder.image_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return horizontalList.size();
}
}
问题: 单击图像时,每次单击时,图像中将显示2个副本。之前的观点发生了变化,显示了新的图像,使计数翻倍。 此外,应用在最多3次点击后崩溃。
请帮忙。
答案 0 :(得分:1)
//I assume you are looping here
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), result, "Title", null);
CameraActivity.mBundle.putString("abc",path);
horizontalList.add(result);
// finish loop....................
//notify the adapter of data changes
recycleViewAdapter.notifyDataSetChanged();
在循环内创建新适配器意味着只能显示单个项目,同时效率低下。循环并在循环中添加元素,并在循环之后,通知适配器数据更改。