在android中拍照录像

1,在manifest中声明使用摄像头

<uses-feature android:name="android.hardware.camera" />,

也可以添加android:required="false"来说明希望有这个设备,但不是必须的。

如果是这样在code中要检查是否有这个设备hasSystemFeature(PackageManager.FEATURE.CAMERA)


2,拍照需要发送的action:  MediaStore.ACTION_IMAGE_CAPTURE

结果以intent返回,        

Bundle extras = intent.getExtras();

mImageBitmap = (Bitmap) extras.get("data");

也可以在发送 MediaStore.ACTION_IMAGE_CAPTURE时带上一个文件名的uri,这样拍照完成后照片存在你指定的位置,

需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

返回的intent中不再携带图片,你可以直接去读取文件


3,拍摄视频要发送MediaStore.ACTION_VIDEO_CAPTURE

返回的intent中把视频uri放在data中,可以直接读出这个uri设置到VideoView上。



package com.example.testactivity;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;


public class MyPhotoActivity extends Activity {

private static final int REQUEST_ID = 1234;
private static final int REQUEST_VIDEO_ID = 1235;
private static final int REQUEST_ID_SELF_FILE = 1236;
private Bitmap mImageBitmap = null;
private ImageView mImageView = null;
private VideoView mVideoView = null;
private Uri mVideoUri = null;
private static final String JPEG_FILE_PREFIX = "IMAGE";
private static final String JPEG_FILE_SUFFIX = "jpg";
private String mCurrentPhotoPath = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_photo);
mImageView = (ImageView)findViewById(R.id.image_view);
mVideoView = (VideoView)findViewById(R.id.video_view);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_photo, menu);
return true;
}

public void takePhoto(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

/* 这一段用于把图片存在固定位置的场景
File f = null;
File dir = null;
try {
//确定文件名
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
//创建文件夹
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
dir = new File(
 Environment.getExternalStoragePublicDirectory(
   Environment.DIRECTORY_PICTURES
 ), 
 "YoonA");
if (dir != null) {
if (!dir.mkdirs()) {
if (!dir.exists()){
Log.d("TEST", "failed to create directory");
dir = null;
}
}
}
}
//根据文件夹和文件名字创建文件
f = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, dir);
mCurrentPhotoPath = f.getAbsolutePath();
//file的Uri加入intent,这样图片就存在了这个file
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); //这样处理file的Uri是file:///开头的,前一篇中FileProvider产生的Uri是content://开头的。
} catch (IOException e) {
Log.d("TEST",e.getMessage());

}
*/

//
startActivityForResult(i, REQUEST_ID);
}
public void takeVideo(View v) {
Intent i = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(i, REQUEST_VIDEO_ID);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ID && resultCode == RESULT_OK) {
if (mCurrentPhotoPath != null) {
setPic();
galleryAddPic();
mCurrentPhotoPath = null;
} else {
       Bundle extras = data.getExtras();
       mImageBitmap = (Bitmap) extras.get("data");
       mImageView.setImageBitmap(mImageBitmap);
}
}
if(requestCode == REQUEST_VIDEO_ID && resultCode == RESULT_OK) {
mVideoView.setVisibility(View.VISIBLE);
   mVideoUri = data.getData();
   mVideoView.setVideoURI(mVideoUri);
   mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {


@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mVideoView.start();
}
   
   });
   mVideoView.start();
}
}

//这种情况下照片存在file中,不随intent返回,所以从文件中获取照片
private void setPic() {


/* There isn't enough memory to open up more than a couple camera photos */
/* So pre-scale the target bitmap into which the file is decoded */


/* Get the size of the ImageView */
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();


/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}


/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;


/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

/* Associate the Bitmap to the ImageView */
mImageView.setImageBitmap(bitmap);
mVideoUri = null;
mImageView.setVisibility(View.VISIBLE);
mVideoView.setVisibility(View.INVISIBLE);
}

        //通知Media Scanner,这样其他app才知道这个file,例如可以在图库中看到
private void galleryAddPic() {
   Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);
   Uri contentUri = Uri.fromFile(f);
   mediaScanIntent.setData(contentUri);
   this.sendBroadcast(mediaScanIntent);
}


}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值