Androidstudio調(diào)用攝像頭拍照并保存照片
本文實(shí)例為大家分享了Androidstudio調(diào)用攝像頭拍照并保存照片的具體代碼,供大家參考,具體內(nèi)容如下
首先在manifest.xmlns文件中聲明權(quán)限
<?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>
在其中我們創(chuàng)建了一個(gè)文件夾,文件夾名字叫做xml,下面存放了file_paths這樣一個(gè)文件
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>
來到主方法中的布局文件,只需要簡(jiǎn)單的一個(gè)按鈕就可以實(shí)現(xiàn),為了將照片顯示出來,添加一個(gè)imgview來將照片顯示在手機(jī)上
布局文件:
<?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>
主方法中只需要將按鈕的點(diǎn)擊事件跳轉(zhuǎn)到Android自帶的系統(tǒng)相機(jī)就可以
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);} ? ? ? ? ? ? ? ? //跳轉(zhuǎn)界面到系統(tǒng)自帶的拍照界面 ? ? ? ? ? ? ? ? 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讓程序運(yùn)行在內(nèi)報(bào)錯(cuò) ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? //將圖片保存 ? ? ? ? ? ? ? ? ? ? ? ? Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); ? ? ? ? ? ? ? ? ? ? ? ? iv_photo.setImageBitmap(bitmap); ? ? ? ? ? ? ? ? ? ? }catch (FileNotFoundException e){ ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? default:break; ? ? ? ? } ? ? } ? }
運(yùn)行效果:
點(diǎn)擊按鈕后:
選擇確定后:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android9?雙屏異顯實(shí)現(xiàn)方式思路
這篇文章主要為大家介紹了Android9?雙屏異顯實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06詳解Android Material Design自定義動(dòng)畫的編寫
這篇文章主要介紹了詳解Android Material Design自定義動(dòng)畫的編寫,其中對(duì)Activity的過渡動(dòng)畫進(jìn)行了重點(diǎn)講解,需要的朋友可以參考下2016-04-04Android日期選擇器實(shí)現(xiàn)年月日三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android日期選擇器實(shí)現(xiàn)年月日三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼
下面小編就為大家分享一篇仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01InputFilter實(shí)現(xiàn)EditText文本輸入過濾器實(shí)例代碼解析
EditText是Android的文本輸入框控件。這篇文章給大家介紹 InputFilter實(shí)現(xiàn)EditText文本輸入過濾器實(shí)例代碼解析,需要的朋友一起看看吧2016-11-11Android下拉列表選項(xiàng)框及指示箭頭動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android下拉列表選項(xiàng)框,及指示箭頭動(dòng)畫的制作代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android中通過MediaStore獲取音樂文件信息方法
這篇文章主要介紹了Android中通過MediaStore獲取音樂文件信息方法,本文講解了獲取歌曲的名稱、歌曲的專輯名、歌曲的歌手名、歌曲文件的全路徑、歌曲文件的名稱、歌曲文件的發(fā)行日期等音樂文件信息的方法,需要的朋友可以參考下2015-04-04基于Android實(shí)現(xiàn)保存圖片到本地并可以在相冊(cè)中顯示出來
App應(yīng)用越來越人性化,不僅界面優(yōu)美而且服務(wù)也很多樣化,操作也非常方便。通過本篇文章給大家介紹基于Android實(shí)現(xiàn)保存圖片到本地并可以在相冊(cè)中顯示出來,對(duì)android保存圖片相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android 優(yōu)化之卡頓優(yōu)化的實(shí)現(xiàn)
這篇文章主要介紹了Android 優(yōu)化之卡頓優(yōu)化的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07Android模擬器"Failed To Allocate memory 8"錯(cuò)誤如何解決
這篇文章主要介紹了Android模擬器"Failed To Allocate memory 8"錯(cuò)誤如何解決的相關(guān)資料,需要的朋友可以參考下2017-03-03