Android打開系統(tǒng)相機并拍照的2種顯示方法
本文實例為大家分享了Android打開系統(tǒng)相機并拍照的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
目標效果:
第二張為點擊第一個按鈕拍照后顯示的,比較模糊,第三章為點擊第二個按鈕拍照后顯示的,比較清楚。
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頁面打開相機并獲取傳遞回來的數(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)相機 startActivityForResult(intent, REQUEST_CAMERA_1); } // 拍照后存儲并顯示圖片 private void openCamera_2() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啟動系統(tǒng)相機 Uri photoUri = Uri.fromFile(new File(mFilePath)); // 傳遞路徑 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系統(tǒng)默認存儲路徑 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)鬟^去的,需要進行獲取 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)相機,所以不需要添加打開相機的權(quán)限,如果想要在別的應(yīng)用里選擇打開系統(tǒng)相機時也出現(xiàn)你的應(yīng)用,需要在AndroidManifest.xml頁面進行設(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> <!-- 注冊相機功能,在別的程序Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);進行啟動相機時也會選擇是否啟動該應(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.運行就可以顯示目標效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實現(xiàn)根據(jù)字母快速定位側(cè)邊欄
這篇文章主要為大家詳細介紹了Android開發(fā)實現(xiàn)根據(jù)字母快速定位側(cè)邊欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01Android TabWidget切換卡的實現(xiàn)應(yīng)用
本篇文章小編為大家介紹,Android TabWidget切換卡的實現(xiàn)應(yīng)用。需要的朋友參考下2013-04-04Android開發(fā)實現(xiàn)自定義水平滾動的容器示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)自定義水平滾動的容器,涉及Android滾動容器的事件響應(yīng)、屬性運算與修改相關(guān)操作技巧,需要的朋友可以參考下2017-10-10app 請求服務(wù)器json數(shù)據(jù)實例代碼
下面小編就為大家分享一篇app 請求服務(wù)器json數(shù)據(jù)實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android?imageVIew實現(xiàn)鏡像旋轉(zhuǎn)的方法
在Android應(yīng)用開發(fā)中,有時候我們需要對ImageView中的圖片進行鏡像旋轉(zhuǎn),以展示不同的效果,本文將介紹如何使用代碼實現(xiàn)ImageView的鏡像旋轉(zhuǎn)效果,這篇文章主要介紹了Android?imageVIew如何做鏡像旋轉(zhuǎn),需要的朋友可以參考下2024-06-06