Android打開(kāi)系統(tǒng)相機(jī)并拍照的2種顯示方法
本文實(shí)例為大家分享了Android打開(kāi)系統(tǒng)相機(jī)并拍照的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:
第二張為點(diǎn)擊第一個(gè)按鈕拍照后顯示的,比較模糊,第三章為點(diǎn)擊第二個(gè)按鈕拍照后顯示的,比較清楚。
1.activity_main.xml頁(yè)面設(shè)置布局。
activity_main.xml頁(yè)面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/btnOpenCamera" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="拍照立即顯示" /> <Button android:id="@+id/btnSavePhoto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:text="拍照存儲(chǔ)后顯示" /> <ImageView android:id="@+id/ivShowPicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_marginTop="130dp" /> </RelativeLayout>
2.MainActivity.java頁(yè)面打開(kāi)相機(jī)并獲取傳遞回來(lái)的數(shù)據(jù),兩種方式第一個(gè)比較模糊,適合小圖,第二個(gè)清楚些。
MainActivity.java頁(yè)面:
package com.example.camera; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { private Button btnOpenCamera, btnSavePhoto; private ImageView ivShowPicture; private static int REQUEST_CAMERA_1 = 1; private static int REQUEST_CAMERA_2 = 2; private String mFilePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 init(); // 控件綁定點(diǎn)擊事件 bindClick(); } // 初始化控件和變量 private void init() { btnOpenCamera = (Button) findViewById(R.id.btnOpenCamera); btnSavePhoto = (Button) findViewById(R.id.btnSavePhoto); ivShowPicture = (ImageView) findViewById(R.id.ivShowPicture); mFilePath = Environment.getExternalStorageDirectory().getPath();// 獲取SD卡路徑 mFilePath = mFilePath + "/" + "temp.png";// 指定路徑 } // 控件綁定點(diǎn)擊事件 private void bindClick() { btnOpenCamera.setOnClickListener(this); btnSavePhoto.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnOpenCamera: // 拍照并顯示圖片 openCamera_1(); break; case R.id.btnSavePhoto: // 拍照后存儲(chǔ)并顯示圖片 openCamera_2(); break; default: break; } } // 拍照并顯示圖片 private void openCamera_1() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動(dòng)系統(tǒng)相機(jī) startActivityForResult(intent, REQUEST_CAMERA_1); } // 拍照后存儲(chǔ)并顯示圖片 private void openCamera_2() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動(dòng)系統(tǒng)相機(jī) Uri photoUri = Uri.fromFile(new File(mFilePath)); // 傳遞路徑 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系統(tǒng)默認(rèn)存儲(chǔ)路徑 startActivityForResult(intent, REQUEST_CAMERA_2); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { // 如果返回?cái)?shù)據(jù) if (requestCode == REQUEST_CAMERA_1) { // 判斷請(qǐng)求碼是否為REQUEST_CAMERA,如果是代表是這個(gè)頁(yè)面?zhèn)鬟^(guò)去的,需要進(jìn)行獲取 Bundle bundle = data.getExtras(); // 從data中取出傳遞回來(lái)縮略圖的信息,圖片質(zhì)量差,適合傳遞小圖片 Bitmap bitmap = (Bitmap) bundle.get("data"); // 將data中的信息流解析為Bitmap類(lèi)型 ivShowPicture.setImageBitmap(bitmap);// 顯示圖片 } else if (requestCode == REQUEST_CAMERA_2) { FileInputStream fis = null; try { fis = new FileInputStream(mFilePath); // 根據(jù)路徑獲取數(shù)據(jù) Bitmap bitmap = BitmapFactory.decodeStream(fis); ivShowPicture.setImageBitmap(bitmap);// 顯示圖片 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { fis.close();// 關(guān)閉流 } catch (IOException e) { e.printStackTrace(); } } } } } }
3.因?yàn)榇蜷_(kāi)的是系統(tǒng)相機(jī),所以不需要添加打開(kāi)相機(jī)的權(quán)限,如果想要在別的應(yīng)用里選擇打開(kāi)系統(tǒng)相機(jī)時(shí)也出現(xiàn)你的應(yīng)用,需要在AndroidManifest.xml頁(yè)面進(jìn)行設(shè)置。
AndroidManifest.xml頁(yè)面:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.camera" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.camera.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 注冊(cè)相機(jī)功能,在別的程序Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);進(jìn)行啟動(dòng)相機(jī)時(shí)也會(huì)選擇是否啟動(dòng)該應(yīng)用 --> <intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
4.運(yùn)行就可以顯示目標(biāo)效果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin標(biāo)準(zhǔn)庫(kù)函數(shù)使用分析及介紹
Kotlin提供了一個(gè)系統(tǒng)庫(kù),是Java庫(kù)的增強(qiáng)。其中有很多函數(shù)在適配了Java的類(lèi)型和方法同時(shí)使用Kotlin的語(yǔ)法。其中一些底層的函數(shù) 是使用比較廣泛的2022-09-09Android開(kāi)發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Android TabWidget切換卡的實(shí)現(xiàn)應(yīng)用
本篇文章小編為大家介紹,Android TabWidget切換卡的實(shí)現(xiàn)應(yīng)用。需要的朋友參考下2013-04-04Android開(kāi)發(fā)實(shí)現(xiàn)自定義水平滾動(dòng)的容器示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)自定義水平滾動(dòng)的容器,涉及Android滾動(dòng)容器的事件響應(yīng)、屬性運(yùn)算與修改相關(guān)操作技巧,需要的朋友可以參考下2017-10-10app 請(qǐng)求服務(wù)器json數(shù)據(jù)實(shí)例代碼
下面小編就為大家分享一篇app 請(qǐng)求服務(wù)器json數(shù)據(jù)實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android?imageVIew實(shí)現(xiàn)鏡像旋轉(zhuǎn)的方法
在Android應(yīng)用開(kāi)發(fā)中,有時(shí)候我們需要對(duì)ImageView中的圖片進(jìn)行鏡像旋轉(zhuǎn),以展示不同的效果,本文將介紹如何使用代碼實(shí)現(xiàn)ImageView的鏡像旋轉(zhuǎn)效果,這篇文章主要介紹了Android?imageVIew如何做鏡像旋轉(zhuǎn),需要的朋友可以參考下2024-06-06