Java乔晓松-android中调用系统拍照功能并显示拍照的图片

android中调用系统拍照功能并显示拍照的图片

如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图

代码如下:

package com.example.myphotos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 2013-6-27 上午10:27:23
 * 
 * @author 乔晓松
 */
public class CameraActivity extends Activity {

	private Button button;
	private ImageView imageView;
	private String fileName;

	@SuppressLint({ "SimpleDateFormat", "SdCardPath" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_camera);
		button = (Button) findViewById(R.id.btn_camera);
		imageView = (ImageView) findViewById(R.id.imageView1);
		File file = new File("/sdcard/myImage/");
		file.mkdirs();// 创建文件夹

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
								startActivityForResult(intent, 1);
			}
		});
		// Intent intent = new Intent();
		// intent.setAction("android.intent.action.MAIN");
		// intent.addCategory("android.intent.category.LAUNCHER");
		// intent.setFlags(0x10200000);
		// intent.setComponent(new ComponentName("com.android.camera",
		// "com.android.camera.Camera"));
		// startActivity(intent);
	}

	@SuppressLint({ "SdCardPath", "SimpleDateFormat" })
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {

				String sdStatus = Environment.getExternalStorageState();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
				Log.v("TestFile",
						"SD card is not avaiable/writeable right now.");
				return;
			}
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg";
			Bundle bundle = data.getExtras();
			Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
			FileOutputStream b = null;

			try {
				b = new FileOutputStream(fileName);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					b.flush();
					b.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里
		}

	}
}

上面获取到的拍摄照片的缩略图,要想获取到拍摄照片的原图,就要在打开相机的时候,把照片保存的地址必须设置好,代码如下:

package com.example.myphotos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 2013-6-27 上午10:27:23
 * 
 * @author 乔晓松
 */
public class CameraActivity extends Activity {

	private Button button;
	private ImageView imageView;
	private String fileName;

	@SuppressLint({ "SimpleDateFormat", "SdCardPath" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_camera);
		button = (Button) findViewById(R.id.btn_camera);
		imageView = (ImageView) findViewById(R.id.imageView1);
		File file = new File("/sdcard/myImage/");
		file.mkdirs();// 创建文件夹

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH.mm.ss");
				String name = format.format(new Date());
				fileName = "/sdcard/myImage/" + name + ".jpg";
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT,
						Uri.fromFile(new File(fileName)));
				startActivityForResult(intent, 1);
			}
		});
		// Intent intent = new Intent();
		// intent.setAction("android.intent.action.MAIN");
		// intent.addCategory("android.intent.category.LAUNCHER");
		// intent.setFlags(0x10200000);
		// intent.setComponent(new ComponentName("com.android.camera",
		// "com.android.camera.Camera"));
		// startActivity(intent);
	}

	@SuppressLint({ "SdCardPath", "SimpleDateFormat" })
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {

			String sdStatus = Environment.getExternalStorageState();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
				Log.v("TestFile",
						"SD card is not avaiable/writeable right now.");
				return;
			}

			Bundle bundle = data.getExtras();
			Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
			FileOutputStream b = null;

			try {
				b = new FileOutputStream(fileName);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					b.flush();
					b.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里
		}

	}
}

 

 

布局文件代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/btn_carema" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/btn_camera"
        android:layout_below="@+id/btn_camera"
        android:layout_marginTop="17dp"
        android:background="#999999"
        tools:ignore="ContentDescription" />

</RelativeLayout>


 

 

现在正在学习阶段,如有错误之处,请大牛们指导,我定修改此文章...


 

 

转载于:https://www.cnblogs.com/dyllove98/p/3172322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值