Android 拍照后显示照片

package cn.testcamera;  
import java.io.File;  
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.provider.MediaStore;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ImageView;  
  
public class MainActivity extends Activity {  
    private Button mButton;  
    private ImageView mImageView;  
    private File mPhotoFile;  
    private String mPhotoPath;  
    public final static int CAMERA_RESULT=8888;  
    public final static String TAG="xx";  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        mButton = (Button) findViewById(R.id.button);  
        mButton.setOnClickListener(new ButtonOnClickListener());  
        mImageView = (ImageView) findViewById(R.id.imageView);  
  
    }  
  
    private class ButtonOnClickListener implements View.OnClickListener {  
        public void onClick(View v) {  
            try {  
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");  
                mPhotoPath="mnt/sdcard/DCIM/Camera/"+getPhotoFileName();  
                mPhotoFile = new File(mPhotoPath);  
                if (!mPhotoFile.exists()) {  
                    mPhotoFile.createNewFile();  
                }  
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));  
                startActivityForResult(intent,CAMERA_RESULT);  
            } catch (Exception e) {  
            }  
        }  
    }  
      
    private String getPhotoFileName() {  
        Date date = new Date(System.currentTimeMillis());  
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");  
        return dateFormat.format(date) + ".jpg";  
    }  
      
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);  
        if (requestCode==CAMERA_RESULT) {  
             Bitmap bitmap = BitmapFactory.decodeFile(mPhotoPath, null);    
             mImageView.setImageBitmap(bitmap);  
        }  
    }  
}  

对于拍摄照片我们可以直接调用系统自带的相机拍照,一般情况下无需我们自己开发相机拍照。 1、当点击按钮后我们可以通过Intent意图启动系统相机 @Override public void onClick(View v) { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String temName=new DateFormat().format("yyMMdd_hhmmss",System.currentTimeMillis())+"_"+(Math.random()*100)+".jpg"; //文件名 image_path=path+File.separator+temName; File file=new File(image_path); if(file.exists()){ file.delete(); } Uri u=Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, u); startActivityForResult(intent, 0); } 在这里设置 intent.putExtra(MediaStore.EXTRA_OUTPUT, u);非常重要,如果不设置这个参数那么我们获取到的图片只是一个经过压缩后的缩略图,只有设置这个才能得到拍摄后的原图。 2、在startActivityForResult(intent, 0);后我们需要重写onActivityResult(int requestCode, int resultCode, Intent data)方法,如果设置了MediaStore.EXTRA_OUTPUT那么我们在这里data返回的是null。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode==RESULT_OK){ File file=new File(image_path); try { Uri uri = Uri.fromFile(file); BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); options.inSampleSize=4; options.inJustDecodeBounds=false; Bitmap map=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), map, null, null); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri)); image.setImageBitmap(map); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 蓝色部分是对图片进行简单的压缩处理,如果不进行处理会出现内存溢出。 红色部分是将图片保存在DCIM文件夹下。 绿色部分是发生一个广播通知系统重新扫描制定文件,这样我们就可以在图库本地图片中看到拍摄的图片。 3、最后记得在清单文件中加入调用系统相机和保存文件权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值