1.捕获照片
1.简单的拍照
获取权限
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" /> //设为true,设备必须拥有相机才能安装应用
... //使用 hasSystemFeature(PackageManager.FEATURE_CAMERA)检测相机是否可用
</manifest>
用相机拍个照片
static final int REQUEST_IMAGE_CAPTURE = 1; //定义请求代码
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { //检测有无可相应Intent的程序
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); //拍照
}
}
获得小样
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data"); //系统自动保存一个小尺寸的图片,key值是"data"
mImageView.setImageBitmap(imageBitmap);
}
}
保存完整图片
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); //定义文件名
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); //获得存储目录
File image = File.createTempFile( //创建临时目录
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath(); //保存绝对目录
return image;
}
创建虚拟文件保存并获得Uri
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
添加进画廊 公开图片
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
设置图片到View
private void setPic(){
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW/targetW,photoH/targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,bmOptions);
mImageView.setImageBitmap(bitmap);
}
2.简单的拍视频
简单的定义Intent
private void dispatchTakeVideoIntent() {
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if(videoIntent.resolveActivity(getPackageManager())!=null){
startActivityForResult(videoIntent,REQUEST_VIDEO_CAPTURE);
}
}
简单的在VideoView播放
if(requestCode == REQUEST_VIDEO_CAPTURE && resultCode == Activity.RESULT_OK){
Uri videoUri = data.getData();
mVideoView.setVideoURI(videoUri);
}