Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法
本文實例為大家分享了Android打開系統(tǒng)相機(jī)并拍照的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:

第二張為點擊第一個按鈕拍照后顯示的,比較模糊,第三章為點擊第二個按鈕拍照后顯示的,比較清楚。
1.activity_main.xml頁面設(shè)置布局。
activity_main.xml頁面:
<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="拍照存儲后顯示" /> <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頁面打開相機(jī)并獲取傳遞回來的數(shù)據(jù),兩種方式第一個比較模糊,適合小圖,第二個清楚些。
MainActivity.java頁面:
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();
// 控件綁定點擊事件
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";// 指定路徑
}
// 控件綁定點擊事件
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:
// 拍照后存儲并顯示圖片
openCamera_2();
break;
default:
break;
}
}
// 拍照并顯示圖片
private void openCamera_1() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動系統(tǒng)相機(jī)
startActivityForResult(intent, REQUEST_CAMERA_1);
}
// 拍照后存儲并顯示圖片
private void openCamera_2() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動系統(tǒng)相機(jī)
Uri photoUri = Uri.fromFile(new File(mFilePath)); // 傳遞路徑
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系統(tǒng)默認(rèn)存儲路徑
startActivityForResult(intent, REQUEST_CAMERA_2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // 如果返回數(shù)據(jù)
if (requestCode == REQUEST_CAMERA_1) { // 判斷請求碼是否為REQUEST_CAMERA,如果是代表是這個頁面?zhèn)鬟^去的,需要進(jìn)行獲取
Bundle bundle = data.getExtras(); // 從data中取出傳遞回來縮略圖的信息,圖片質(zhì)量差,適合傳遞小圖片
Bitmap bitmap = (Bitmap) bundle.get("data"); // 將data中的信息流解析為Bitmap類型
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.因為打開的是系統(tǒng)相機(jī),所以不需要添加打開相機(jī)的權(quán)限,如果想要在別的應(yīng)用里選擇打開系統(tǒng)相機(jī)時也出現(xiàn)你的應(yīng)用,需要在AndroidManifest.xml頁面進(jìn)行設(shè)置。
AndroidManifest.xml頁面:
<?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>
<!-- 注冊相機(jī)功能,在別的程序Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);進(jìn)行啟動相機(jī)時也會選擇是否啟動該應(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.運行就可以顯示目標(biāo)效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)升級AGP7.0后的一些適配方法技巧
這篇文章主要為大家介紹了升級AGP7.0后的一些適配方法技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android ANR(Application Not Responding)的分析
這篇文章主要介紹了Android ANR(Application Not Responding)的分析的相關(guān)資料,這里說明什么原因出現(xiàn)應(yīng)用程序的強(qiáng)制關(guān)閉,并說明該如何避免,需要的朋友可以參考下2017-08-08
Android LayoutTransiton實現(xiàn)簡單的錄制按鈕
這篇文章主要介紹了Android LayoutTransiton實現(xiàn)簡單的錄制按鈕,主要實現(xiàn)開始,暫停,停止和顯示錄制時間長度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android 使用AsyncTask實現(xiàn)斷點續(xù)傳
這篇文章主要介紹了Android 使用AsyncTask實現(xiàn)斷點續(xù)傳的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-05-05
Android開發(fā)之?dāng)?shù)據(jù)的存儲方式詳解
本篇文章主要介紹了Android開發(fā)之?dāng)?shù)據(jù)的存儲方式,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。2016-11-11
Android使用Theme自定義Activity進(jìn)入退出動畫的方法
這篇文章主要介紹了Android使用Theme自定義Activity進(jìn)入退出動畫的方法,涉及Android的Activity屬性設(shè)置與資源操作技巧,需要的朋友可以參考下2016-07-07
Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法詳解
這篇文章主要介紹了Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法,結(jié)合實例形式分析了Android傳感器的調(diào)用方法、參數(shù)含義及具體使用技巧,需要的朋友可以參考下2017-08-08

