基本信息
源码名称:Android用Intent实现Camera 拍照功能
源码大小:0.05M
文件格式:.rar
开发语言:Java
更新时间:2014-01-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


package com.gel.camera;

import java.io.File;
import java.io.FileInputStream;
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.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CameraIntentDemoActivity extends Activity {
    
	private static final int MEDIA_TYPE_IMAGE = 1;
	private static final int MEDIA_TYPE_VIDEO = 2;	
	private Uri fileUri;
	
	private static Uri getOutputMediaFileUri(int type) {
		return Uri.fromFile(getOutputMediaFile(type));
	}
	
	private static File getOutputMediaFile(int type){
	    // To be safe, you should check that the SDCard is mounted
	    // using Environment.getExternalStorageState() before doing this.

	    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
	    // This location works best if you want the created images to be shared
	    // between applications and persist after your app has been uninstalled.

	    // Create the storage directory if it does not exist
	    if (! mediaStorageDir.exists()){
	        if (! mediaStorageDir.mkdirs()){
	            Log.d("MyCameraApp", "failed to create directory");
	            return null;
	        }
	    }

	    // Create a media file name
	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    File mediaFile;
	    if (type == MEDIA_TYPE_IMAGE){
	        mediaFile = new File(mediaStorageDir.getPath()   File.separator  
	        "IMG_"  timeStamp   ".jpg");
	    } else if(type == MEDIA_TYPE_VIDEO) {
	        mediaFile = new File(mediaStorageDir.getPath()   File.separator  
	        "VID_"  timeStamp   ".mp4");
	    } else {
	        return null;
	    }

	    return mediaFile;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnTakePhoto = (Button)this.findViewById(R.id.btnTakePhoto);
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				// create a file to save the image
				fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
				// set the image file name
				cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
				// start the image capture Intent
				startActivityForResult(cameraIntent, 0);
			}
		});
        
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		try	{
			if (requestCode != 0) {
				return;
			}
			Bitmap bmp = null;
			super.onActivityResult(requestCode, resultCode, data);
			if (data != null) {
				Bundle extras = data.getExtras();
				bmp = (Bitmap)extras.get("data");				
			}
			else {
				FileInputStream fis = new FileInputStream(fileUri.getPath());
				bmp = BitmapFactory.decodeStream(fis);
			}
			
			ImageView imag = (ImageView)this.findViewById(R.id.imgvTakePhoto);
			imag.setImageBitmap(bmp);
			
			
		} catch (Exception e){
			System.out.println(e.getMessage());
		}
		
	}
}