Androidstudio调用摄像头拍照并保存照片
作者:修24352 发布时间:2022-01-23 18:33:57
标签:Androidstudio,摄像头
本文实例为大家分享了Androidstudio调用摄像头拍照并保存照片的具体代码,供大家参考,具体内容如下
首先在manifest.xmlns文件中声明权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.takephoto">
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TakePhoto">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="com.example.takephoto.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
在其中我们创建了一个文件夹,文件夹名字叫做xml,下面存放了file_paths这样一个文件
path.xml文件中存放的代码为
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="my_images"
path="."/>
</paths>
来到主方法中的布局文件,只需要简单的一个按钮就可以实现,为了将照片显示出来,添加一个imgview来将照片显示在手机上
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="📸"
android:id="@+id/btn_takephoto"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/img_photo"/>
</LinearLayout>
主方法中只需要将按钮的点击事件跳转到Android自带的系统相机就可以
package com.example.takephoto;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Intent;
import android.content.RestrictionEntry;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
final int TAKE_PHOTO=1;
ImageView iv_photo;
Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_1=findViewById(R.id.btn_takephoto);
iv_photo=findViewById(R.id.img_photo);
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File output=new File(getExternalCacheDir(),"output_image.jpg");
try {
if (output.exists()){
output.delete();
}
output.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
if (Build.VERSION.SDK_INT>=24){
//图片的保存路径
imageUri= FileProvider.getUriForFile(MainActivity.this,"com.example.takephoto.fileprovider",output);
}
else { imageUri=Uri.fromFile(output);}
//跳转界面到系统自带的拍照界面
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,TAKE_PHOTO);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case TAKE_PHOTO:
if (resultCode==RESULT_OK){
// 使用try让程序运行在内报错
try {
//将图片保存
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
iv_photo.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
break;
default:break;
}
}
}
运行效果:
点击按钮后:
选择确定后:
来源:https://blog.csdn.net/m0_55995292/article/details/117382491


猜你喜欢
- 前言面向切面编程,利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发
- 在Android中子线程是不能更新ui的。所以我们要通过其他方式来动态改变ui视图,1、runOnUiThreadactivity提供的一个
- for循环for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。for循环执行的次数是在执行前就确定的。语法格式如下:for(
- 前言众所周知在spring boot内,设置session过期时间只需在application.properties内添加server.se
- 话不多说,请看代码:using System;using System.Web;using System.Drawing;using Sys
- 1.新建springBoot项目在前面有两种方式2.加入thymeleaf模板引擎SpringBoot推荐使用thymeleaf模板引擎语法
- 问题:在Spring Boot中使用JpaRepository的deleteById(ID id)方法删除数据时,首先要使用existsBy
- 修改加密和验证方法/** * 生成BCryptPasswordEncoder密码 *
- 在Web的应用方面有js的插件实现自动完成(或叫智能提示)功能,但在WinForm窗体应用方面就没那么好了。TextBox控件本身是提供了一
- 对于从事Android开发的人来说,遇到ANR(Application Not Responding)是比较常见的问题。一般情况下,如果有A
- 提示:IntelliJ IDEA以下简称IDEA;####IntelliJ IDEA 配置git:需要的材料:一、git.exe二、配置gi
- 本文实例讲述了Spring实战之属性占位符配置器用法。分享给大家供大家参考,具体如下:一 配置文件<?xml version=&quo
- 一、背景新做了一个的需求,需要在SpringBoot项目中引入了多个依赖,然后就感觉idea下载依赖包的时间很漫长,然后我就网上找了解决办法
- 串口通信(Serial Communications)是指外设和计算机间通过数据信号线、地线等按位(bit)进行传输数据的一种通信方式,属于
- 本文实例讲述了java方法重写,分享给大家供大家参考。具体分析如下:一、方法的重写概述:1、在子类中可以根据需要对从基类中继承来的方法进行重
- 本文实例为大家分享了Android原生视频播放VideoView的具体代码,供大家参考,具体内容如下布局文件activity_video.x
- 背景在一些业务场景, 往往需要自定义异常来满足特定的业务, 主流用法是在catch里抛出异常, 例如:public void deal()
- 完整代码已上传到GitHub。Web端体验地址:http://47.116.72.33/(只剩一个月有效期)apk下载地址:https://
- 随着C#的发展,该语言内容不断丰富,开发变得更加方便快捷,C# 的锋利尽显无疑。C# 语言从诞生起就是强类型语言,这一性质到今天不曾改变,我
- 本文实现了C#隐式运行CMD命令的功能。下图是实例程序的主画面。在命令文本框输入DOS命令,点击“Run”按钮,在下面的文本框中输出运行结果