经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Androidstudio调用摄像头拍照并保存照片
来源:jb51  时间:2022/3/29 9:46:26  对本文有异议

本文实例为大家分享了Androidstudio调用摄像头拍照并保存照片的具体代码,供大家参考,具体内容如下

首先在manifest.xmlns文件中声明权限

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? package="com.example.takephoto">
  4. ?
  5. ? ? <application
  6. ? ? ? ? android:requestLegacyExternalStorage="true"
  7. ? ? ? ? android:allowBackup="true"
  8. ? ? ? ? android:icon="@mipmap/ic_launcher"
  9. ? ? ? ? android:label="@string/app_name"
  10. ? ? ? ? android:roundIcon="@mipmap/ic_launcher_round"
  11. ? ? ? ? android:supportsRtl="true"
  12. ? ? ? ? android:theme="@style/Theme.TakePhoto">
  13. ? ? ? ? <activity android:name=".MainActivity">
  14. ? ? ? ? ? ? <intent-filter>
  15. ? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />
  16. ?
  17. ? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />
  18. ? ? ? ? ? ? </intent-filter>
  19. ? ? ? ? </activity>
  20. ? ? ? ? <provider
  21. ? ? ? ? ? ? android:authorities="com.example.takephoto.fileprovider"
  22. ? ? ? ? ? ? android:name="androidx.core.content.FileProvider"
  23. ? ? ? ? ? ? android:exported="false"
  24. ? ? ? ? ? ? android:grantUriPermissions="true">
  25. ? ? ? ? <meta-data
  26. ? ? ? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS"
  27. ? ? ? ? ? ? android:resource="@xml/file_paths"/>
  28. ? ? ? ? </provider>
  29. ? ? </application>
  30. ? ? <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  31. ? ? ? ? <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  32. ?
  33. </manifest>

在其中我们创建了一个文件夹,文件夹名字叫做xml,下面存放了file_paths这样一个文件

path.xml文件中存放的代码为

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3. ? ? <external-path
  4. ? ? ? ? name="my_images"
  5. ? ? ? ? path="."/>
  6. ?
  7. </paths>

来到主方法中的布局文件,只需要简单的一个按钮就可以实现,为了将照片显示出来,添加一个imgview来将照片显示在手机上

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? xmlns:app="http://schemas.android.com/apk/res-auto"
  4. ? ? xmlns:tools="http://schemas.android.com/tools"
  5. ? ? android:layout_width="match_parent"
  6. ? ? android:layout_height="match_parent"
  7. ? ? android:orientation="vertical"
  8. ? ? tools:context=".MainActivity">
  9. ?
  10. ? ? <Button
  11. ? ? ? ? android:layout_width="match_parent"
  12. ? ? ? ? android:layout_height="wrap_content"
  13. ? ? ? ? android:text="??"
  14. ? ? ? ? android:id="@+id/btn_takephoto"/>
  15. ? ? <ImageView
  16. ? ? ? ? android:layout_width="match_parent"
  17. ? ? ? ? android:layout_height="wrap_content"
  18. ? ? ? ? android:id="@+id/img_photo"/>
  19. ?
  20. </LinearLayout>

主方法中只需要将按钮的点击事件跳转到Android自带的系统相机就可以

  1. package com.example.takephoto;
  2. ?
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.core.content.FileProvider;
  5. ?
  6. import android.content.Intent;
  7. import android.content.RestrictionEntry;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.provider.MediaStore;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.ImageView;
  17. ?
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. ?
  22. public class MainActivity extends AppCompatActivity {
  23. ? ? final int TAKE_PHOTO=1;
  24. ? ? ImageView iv_photo;
  25. ? ? Uri imageUri;
  26. ? ? @Override
  27. ? ? protected void onCreate(Bundle savedInstanceState) {
  28. ? ? ? ? super.onCreate(savedInstanceState);
  29. ? ? ? ? setContentView(R.layout.activity_main);
  30. ? ? ? ? Button btn_1=findViewById(R.id.btn_takephoto);
  31. ? ? ? ? iv_photo=findViewById(R.id.img_photo);
  32. ? ? ? ? btn_1.setOnClickListener(new View.OnClickListener() {
  33. ? ? ? ? ? ? @Override
  34. ? ? ? ? ? ? public void onClick(View v) {
  35. ? ? ? ? ? ? ? ? File output=new File(getExternalCacheDir(),"output_image.jpg");
  36. ? ? ? ? ? ? ? ? try {
  37. ? ? ? ? ? ? ? ? ? ? if (output.exists()){
  38. ? ? ? ? ? ? ? ? ? ? ? ? output.delete();
  39. ? ? ? ? ? ? ? ? ? ? }
  40. ? ? ? ? ? ? ? ? ? ? output.createNewFile();
  41. ? ? ? ? ? ? ? ? }catch (IOException e){
  42. ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  43. ? ? ? ? ? ? ? ? }
  44. ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT>=24){
  45. //图片的保存路径
  46. ? ? ? ? ? ? ? ? ? ? imageUri= FileProvider.getUriForFile(MainActivity.this,"com.example.takephoto.fileprovider",output);
  47. ? ? ? ? ? ? ? ? }
  48. ? ? ? ? ? ? ? ? else { imageUri=Uri.fromFile(output);}
  49. ? ? ? ? ? ? ? ? //跳转界面到系统自带的拍照界面
  50. ? ? ? ? ? ? ? ? Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
  51. ? ? ? ? ? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
  52. ? ? ? ? ? ? ? ? startActivityForResult(intent,TAKE_PHOTO);
  53. ? ? ? ? ? ? }
  54. ? ? ? ? });
  55. ? ? }
  56. ? ? protected ?void onActivityResult(int requestCode,int resultCode,Intent data) {
  57. ? ? ? ? super.onActivityResult(requestCode, resultCode, data);
  58. ? ? ? ? switch (requestCode){
  59. ? ? ? ? ? ? case TAKE_PHOTO:
  60. ? ? ? ? ? ? ? ? if (resultCode==RESULT_OK){
  61. ? ? ? ? ? ? ? ? ? ? // 使用try让程序运行在内报错
  62. ? ? ? ? ? ? ? ? ? ? try {
  63. ? ? ? ? ? ? ? ? ? ? ? ? //将图片保存
  64. ? ? ? ? ? ? ? ? ? ? ? ? Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
  65. ? ? ? ? ? ? ? ? ? ? ? ? iv_photo.setImageBitmap(bitmap);
  66. ? ? ? ? ? ? ? ? ? ? }catch (FileNotFoundException e){
  67. ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  68. ? ? ? ? ? ? ? ? ? ? }
  69. ? ? ? ? ? ? ? ? }
  70. ? ? ? ? ? ? ? ? break;
  71. ? ? ? ? ? ? default:break;
  72. ? ? ? ? }
  73. ? ? }
  74. ?
  75. }

运行效果:

点击按钮后:

选择确定后:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号