安卓RecyclerView实现系统相册相机添加多个图片
1.这个是弹出一个弹框选择相册还是相机
private void showselectDialog() {
Dialog mDialog = new Dialog(this, R.style.dialogStyle);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true); //点击框外,框退出
Window window = mDialog.getWindow();
window.setGravity(Gravity.BOTTOM); //位于底部
window.setWindowAnimations(R.style.dialog_share); //弹出动画
View inflate = View.inflate(this, R.layout.dialog_picture_view, null);
TextView opencamera=inflate.findViewById(R.id.open_picture_camera);
TextView openalbum=inflate.findViewById(R.id.open_picture_album);
opencamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 请求相机权限
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, 0);
Toast.makeText(activity, "拍照需要用到手机相机权限", Toast.LENGTH_SHORT).show();
} else {
// 已获得权限,执行相应的操作
showopencamera();
}
mDialog.dismiss(); }
});
openalbum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 检查并请求图片权限
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
Toast.makeText(activity, "获取相册图片,需要开启文件权限", Toast.LENGTH_SHORT).show();
} else {
// 权限已获得,执行读取图片的代码
showopenalbum();
}
mDialog.dismiss();
}
});
window.setContentView(inflate);
//横向充满
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
mDialog.show();
}`
2.` 这里是activity的回调方法.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("请求码:", "onActivityResult: "+requestCode);
if (data!=null){
//相机的拍照效果
if (requestCode==0){
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap)bundle.get("data"); bitmaplist.add(Imagersource(bitmap));
}else if(requestCode == 1){
//图片的拍照效果
Uri imageUri = data.getData();
//getRealPathFromURI 从我们的相册里获取图片 写在下方了
String imagePath = getRealPathFromURI(imageUri);
getImageFromUri(activity,imageUri);
}
brforeImgAdapter.setList(这里写的是你List资源);
maintainimagerrv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
maintainimagerrv.setAdapter(brforeImgAdapter);
brforeImgAdapter.notifyDataSetChanged();
}
}`
//从我们的相册里获取图片
public Bitmap getImageFromUri(Context context, Uri imageUri) {
Bitmap bitmap = null;
try {
Cursor cursor = context.getContentResolver().query(imageUri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
// 从路径加载图片为Bitmap
bitmap = BitmapFactory.decodeFile(path);
// Bitmap roundedCornerBitmap = getRoundedCornerBitmap(bitmap, 400);
bitmaplist.add(Imagersource(bitmap));
Log.d("TAG", "getImageFromUri: "+bitmap);
} else {
Log.e("Error", "Failed to get cursor from image URI: " + imageUri);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
3.弹框布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:background="#fff"
>
<TextView
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:id="@+id/open_picture_camera"
android:layout_weight="1"
android:gravity="center"
android:text="@string/det_msg_64"
android:textSize="18sp"
android:textColor="#333"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<View
android:background="#AFAEAE"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:id="@+id/open_picture_album"
android:layout_weight="1"
android:gravity="center"
android:text="@string/det_msg_65"
android:textSize="18sp"
android:textColor="#333"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
4.RecyclerView布局
<LinearLayout
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="14sp"
android:textColor="#3D3D3D"
android:drawableStart="@mipmap/red_hind"
android:text="@string/det_msg_55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/brfore_image_rv"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="80dp"/>
</LinearLayout>
5.RecyclerView布局里面的二个布局 因为要实现的是在RecyclerView里面添加一个添加按钮文章来源:https://www.toymoban.com/news/detail-830486.html
//这个是展示的图片的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp">
<ImageView
android:background="@drawable/abcdefg"
android:scaleType="fitXY"
android:id="@+id/brfore_image_item_img"
android:layout_width="80dp"
android:layout_height="80dp"/>
<ImageView
android:id="@+id/brfore_image_error"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/image_error" />
</RelativeLayout>
</LinearLayout>
//这个是添加按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:gravity="center"
android:layout_height="80dp"
android:id="@+id/add_btn_rl"
android:background="@drawable/fragment_main_bg"
>
<Button
android:text="添加按钮"
android:id="@+id/addImageView"
android:layout_gravity="center"
android:layout_width="25dp"
android:layout_height="25dp"/>
</RelativeLayout>
6.这个是我们的适配器内容文章来源地址https://www.toymoban.com/news/detail-830486.html
public class BrforeImgAdapter extends RecyclerView.Adapter<BrforeImgAdapter.MyViewHolder> {
private Context context;
private List<Bitmap> list;
private static final int TYPE_ITEM = 0;
private static final int TYPE_ADD = 1;
public BrforeImgAdapter(FragmentActivity activity) {
this.context=activity;
}
public void setList(List<Bitmap> bitmaiList) {
this.list=bitmaiList;
}
到了这里,关于安卓RecyclerView实现系统相册相机添加多个图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!