在App中使用相機的兩種方式: java
一、調用系統相機、或者是具備相機功能的應用 android
二、自定義相機 code
調用系統相機的方式: 圖片
打開相機 get
//默認的返回圖片,返回的只是縮略圖 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQ_CAMERA);
返回路徑: it
if(resultCode != RESULT_OK){ return; } if(requestCode == REQ_CAMERA){ if(data != null){ Bundle bundle = data.getExtras(); //Bundle裏返回的是縮略圖 Bitmap bitmap = (Bitmap)bundle.get("data"); mImageView.setImageBitmap(bitmap); } }
//指定圖片返回路徑 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri uri = Uri.fromFile(new File(mFilePath)); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, REQ_CAMERA_ORIGINAL_IMAGE);
else if(requestCode == REQ_CAMERA_ORIGINAL_IMAGE){ //獲取原圖 Bitmap bitmap = BitmapFactory.decodeFile(mFilePath); mImageView.setImageBitmap(bitmap); }
三、註冊Camera功能: io
新建一個module,在Activity裏添加以下注冊代碼: class
<activity android:name=".MyCameraActivity"> <intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
即可以開啓自定義相機的第一步了。 module