Android實現調用攝像頭拍照并存儲照片
1、前期準備
需要在Manifest中添加相關權限
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、主要方法
1、需要使用Intent調用攝像頭
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//調用Camera startActivityForResult(intent, Activity.RESULT_OK);//如果正常,則返回 Activity.RESULT_OK 本質為 int類型的1
2、需要檢查SD卡(外部存儲)狀態(tài)
Environment.MEDIA_MOUNTED
sd卡在手機上正常使用狀態(tài)
Environment.MEDIA_UNMOUNTED
用戶手工到手機設置中卸載sd卡之后的狀態(tài)
Environment.MEDIA_REMOVED
用戶手動卸載,然后將sd卡從手機取出之后的狀態(tài)
Environment.MEDIA_BAD_REMOVAL
用戶未到手機設置中手動卸載sd卡,直接撥出之后的狀態(tài)
Environment.MEDIA_SHARED
手機直接連接到電腦作為u盤使用之后的狀態(tài)
Environment.MEDIA_CHECKINGS
手機正在掃描sd卡過程中的狀態(tài)
在代碼中的判斷
String sdStatus = Environment.getExternalStorageState(); if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){ System.out.println(" ------------- sd card is not avaiable ---------------"); return; }
3、獲取圖片及其壓縮圖片
String name = "photo.jpg";//定義圖片名稱 Bundle bundle = data.getExtras();//data為onActivityResult中的intent類型的參數 Bitmap bitmap = (Bitmap) bundle.get("data");//bitmap // File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"); // file.mkdirs(); //創(chuàng)建文件夾 // String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name; String fileName = "sdcard"+"/"+name;//初始化文件路徑 FileOutputStream fos =null;//初始化文件輸出流 try { // System.out.println(fileName); // 測試用,查看文件路徑 fos=new FileOutputStream(fileName); // 輸出文件到外部存儲 //今天第一次正視這個bitmap.compress()方法,它用來壓縮圖片大小。 /* 這個方法有三個參數: Bitmap.CompressFormat format 圖像的壓縮格式; int quality 圖像壓縮率,0-100。 0 壓縮100%,100意味著不壓縮; OutputStream stream 寫入壓縮數據的輸出流; public boolean compress(CompressFormat format, int quality, OutputStream stream) {} 返回值boolean類型 如果成功地把壓縮數據寫入輸出流,則返回true。 */ bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { fos.flush(); //釋放輸出流 fos.close(); } catch (IOException e) { e.printStackTrace(); } }
3、案例展示
1、Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_take_photo" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="take photo" android:id="@+id/takephoto" /> <ImageView android:layout_below="@+id/takephoto" android:layout_width="400dp" android:layout_height="400dp" android:id="@+id/picture" /> </RelativeLayout>
2、MainActivity
package icu.whatsblog.CameraCrop; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Environment; import android.provider.MediaStore; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import lee.suk.cameracrop.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((Button) findViewById(R.id.takephoto)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK){ String sdStatus = Environment.getExternalStorageState(); if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){ System.out.println(" ------------- sd card is not avaiable ---------------"); return; } String name = "photo.jpg"; Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data"); // File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"); // file.mkdirs(); //創(chuàng)建文件夾 // String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name; String fileName = "sdcard"+"/"+name; FileOutputStream fos =null; try { System.out.println(fileName); fos=new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } ((ImageView) findViewById(R.id.picture)).setImageBitmap(bitmap); } } }
到此這篇關于Android實現調用攝像頭拍照并存儲照片的文章就介紹到這了,更多相關Android調用攝像頭拍照并存儲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android 動態(tài)添加Fragment的實例代碼
這篇文章主要介紹了Android 動態(tài)添加Fragment的實例代碼的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08Android開發(fā)入門環(huán)境快速搭建實戰(zhàn)教程
最近想重新學習下Android,學習之前開發(fā)環(huán)境的搭建是個首先要解決的問題,所以下面這篇文章主要給大家介紹了Android開發(fā)環(huán)境搭建的相關資料,文中將實現的步驟一步步介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11Android 系統(tǒng)實現多種開機動畫和logo切換功能
這篇文章主要介紹了android 系統(tǒng)實現多種開機動畫和logo切換功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12