android app调用系统相机后拍照,将照片路径信息存到数据库,并在recyclerview显示拍到的照片

时间: 2023-12-17 18:05:35 浏览: 29
实现这个功能需要以下几个步骤: 1. 在 AndroidManifest.xml 文件中添加相机和文件读写权限。 ``` <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 2. 创建一个拍照按钮,并在点击事件中调用系统相机。 ```java // 启动系统相机 public void startCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { File photoFile = createImageFile(); if (photoFile != null) { Uri photoUri = FileProvider.getUriForFile(this, "com.example.fileprovider", photoFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, REQUEST_CODE_CAMERA); } } } // 创建图片文件 public File createImageFile() { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); try { File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir); mCurrentPhotoPath = imageFile.getAbsolutePath(); return imageFile; } catch (IOException e) { e.printStackTrace(); } return null; } // 处理拍照结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { String photoPath = mCurrentPhotoPath; savePhotoPath(photoPath); // 将照片路径保存到数据库 mAdapter.addData(photoPath); // 将照片路径添加到 RecyclerView 中显示 } } ``` 3. 在 Activity 中创建一个 RecyclerView,并通过 Adapter 将数据绑定到 RecyclerView 上。 ```java // 初始化 RecyclerView public void initRecyclerView() { mRecyclerView = findViewById(R.id.recyclerview); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mAdapter = new PhotoAdapter(this); mRecyclerView.setAdapter(mAdapter); } // 创建 Adapter public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.ViewHolder> { private Context mContext; private List<String> mPhotoPaths; public PhotoAdapter(Context context) { mContext = context; mPhotoPaths = new ArrayList<>(); } public void setData(List<String> photoPaths) { mPhotoPaths.clear(); mPhotoPaths.addAll(photoPaths); notifyDataSetChanged(); } public void addData(String photoPath) { mPhotoPaths.add(0, photoPath); notifyItemInserted(0); mRecyclerView.scrollToPosition(0); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_photo, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { String photoPath = mPhotoPaths.get(position); Glide.with(mContext).load(photoPath).into(holder.ivPhoto); } @Override public int getItemCount() { return mPhotoPaths.size(); } public class ViewHolder extends RecyclerView.ViewHolder { ImageView ivPhoto; public ViewHolder(View itemView) { super(itemView); ivPhoto = itemView.findViewById(R.id.iv_photo); } } } ``` 4. 将拍摄的照片路径保存到数据库中。 ```java // 将照片路径保存到数据库 public void savePhotoPath(String photoPath) { SQLiteDatabase db = mDbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(PhotoEntry.COLUMN_NAME_PATH, photoPath); db.insert(PhotoEntry.TABLE_NAME, null, values); db.close(); } ``` 5. 在 Activity 的 onCreate() 方法中初始化 RecyclerView 和数据库。 ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initRecyclerView(); mDbHelper = new PhotoDbHelper(this); loadPhotoPaths(); // 从数据库中加载照片路径并显示到 RecyclerView 上 } ``` 6. 从数据库中加载照片路径并显示到 RecyclerView 上。 ```java // 从数据库中加载照片路径并显示到 RecyclerView 上 public void loadPhotoPaths() { SQLiteDatabase db = mDbHelper.getReadableDatabase(); String[] projection = { PhotoEntry._ID, PhotoEntry.COLUMN_NAME_PATH }; Cursor cursor = db.query( PhotoEntry.TABLE_NAME, projection, null, null, null, null, PhotoEntry._ID + " DESC" ); List<String> photoPaths = new ArrayList<>(); while (cursor.moveToNext()) { String photoPath = cursor.getString(cursor.getColumnIndexOrThrow(PhotoEntry.COLUMN_NAME_PATH)); photoPaths.add(photoPath); } mAdapter.setData(photoPaths); cursor.close(); db.close(); } ``` 这样就可以实现在 Android App 中调用系统相机后拍照,将照片路径信息存到数据库,并在 RecyclerView 显示拍到的照片。

相关推荐

最新推荐

recommend-type

Android RecyclerView实现数据列表展示效果

主要为大家详细介绍了Android RecyclerView实现数据列表展示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Android studio so库找不到问题解决办法

主要介绍了Android studio so库找不到问题解决办法的相关资料,希望通过本文能帮助到大家解决出现的这种问题,需要的朋友可以参考下
recommend-type

Android判断当前App是在前台还是在后台

主要为大家详细介绍了Android判断当前App是在前台还是在后台的方法,感兴趣的小伙伴们可以参考一下
recommend-type

使用Android Studio实现为系统级的app签名

主要介绍了使用Android Studio实现为系统级的app签名,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python爬虫进阶之多线程爬取数据并保存到数据库

顿时觉得自己有行了,准备用appium登录QQ爬取列表中好友信息,接踵而来的是一步一步的坑,前期配置无数出错,安装之后连接也是好多错误,把这些错误解决之后,找APPactivity的值又让我绕了一大圈,找到值后又在权限...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。