安卓RecyclerView实现系统相册相机添加多个图片

这篇具有很好参考价值的文章主要介绍了安卓RecyclerView实现系统相册相机添加多个图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

安卓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里面添加一个添加按钮

//这个是展示的图片的布局
<?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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android打开系统相机或从相册中选择图片并显示

    xml代码 展示  java代码 实机演示  

    2024年02月11日
    浏览(42)
  • Android相册选择图片、相机拍照上传功能实现(上)

    先上效果图 下面就来说一下相册选择图片和相机拍照的实现 相册选择图片很简单,只需要通过 Intent 设置拉起就可以了 Intent 拉起相册 /** 打开相册 @param type 打开类型区分码(type是我用来区分回调的) / private void openGallery(int type) { Intent gallery = new Intent(Intent.ACTION_PICK); galler

    2024年04月16日
    浏览(43)
  • Flutter实现点击头像更新头像的功能,本地相册选取和调用相机两种方式的实现

    Flutter实现点击头像更新头像的功能,需要实现本地相册选取和调用相机两种方式的实现。 用InkWell包住了我们的头像,这样来监听点击头像的事件 InkWell 是一个非常常用且功能强大的小部件。InkWell 可以用于为Flutter应用添加水波纹效果,以及捕获和处理用户的点击手势。 In

    2024年02月15日
    浏览(46)
  • 微信小程序 图片 相机 二维码 动画相关API(实现选择相册、拍照、录像、动画)

    本文探讨微信小程序 图片 相机 二维码 动画相关API(实现选择相册、拍照、录像、动画)功能,带有示例代码

    2024年02月04日
    浏览(40)
  • uniapp+uView 实现自定义水印相机,拍完照片给图片加水印,从相册选择图片加水印功能

    样式图如上所示 页面分为取景框和拍照完成后预览,本功能设计到,公共上传组件,相机也页面,获取定位地址,页面中如何用该上传组件 UI实现 取景界面分为上下两个部分,上部分为camera取景框组件,下部分为操作区域。 取景框组件上的关闭和水印,以及拍完照片后的略

    2024年04月14日
    浏览(82)
  • uniapp 水印/相机/授权位置、相机、相册权限

    自定义相机水印 授权     // 手动授权【摄像头】     function getAuth() {         // 获取摄像头权限         uni.getSetting({             success(res) {                 console.log(\\\"auth res\\\", res);                 const authSetting = res.authSetting;                 if (auth

    2024年02月02日
    浏览(34)
  • 手机抓包-安卓添加Burp系统根证书

    一、准备证书 1、下载或导出burp证书 1)下载 burp开启8080端口监听,访问127.0.0.1:8080,下载证书   2)导出 在Proxy的Options中导出证书 2、转换证书格式 使用openssl将.der转换为.pem格式,windows中没有openssl.exe工具的可以在在linux完成 使用openssl 获取有效的系统证书文件名 使用open

    2024年02月08日
    浏览(39)
  • Android中实现RecyclerView,并对item及其多个子控件的点击事件监听

    目录 背景 实现RecyclerView 第一步、 新建item的xml 第二步、在activity的布局中引入 RecyclerView 第三步、新建一个adapter   第四步、在activity中初始化绑定adapter即可 实现item及其多个子组件点击事件监听 第一步、 适配器中创建监听对象 第二步、适配器中绑定监听item和子组件 第三

    2024年02月19日
    浏览(48)
  • (一)Qt下实现多个海康工业相机内触发采集回调取流显示

    提示:这里是该系列文章的所有文章的目录 第一章:(一)Qt下实现多个海康工业相机内触发采集回调取流显示 第二章:(二)Qt下多线程实现多个海康工业相机内触发采集回调取流显示 在我之前所记录的关于海康工业相机的系列文章中 ,讲述的是使用外触发采集模式中的

    2024年02月16日
    浏览(41)
  • 随手笔记——根据相机位姿实现多个RGBD图像点云的叠加

    程序主要实现:(1). 根据内参计算一对 RGB-D 图像对应的点云;(2). 根据各张图的相机位姿(也就是外参),把点云加起来,组成地图。

    2024年02月16日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包