網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2023-05-08 Linux?C/C++?timeout命令實現運行具有時間限制功能_C 語言
- 2022-10-17 android中px、sp與dp之間進行轉換詳解_Android
- 2022-08-30 DOM節點對象 、獲取節點、節點屬性、動態操作DOM節點、toList1.0/toList2.0、
- 2023-02-25 Golang合并yaml文件過程逐步講解_Golang
- 2022-08-31 C++淺析類與對象基礎點_C 語言
- 2023-03-16 C語言進階之字符串查找庫函數詳解_C 語言
- 2022-11-18 Python?ndarray?數組的變形詳情_python
- 2022-12-29 Kotlin?Service服務組件開發詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支