package com.example.camera;
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.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
private ImageButton img_btn;
private Button btn;
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
// 创建一个以当前时间为名称的文件
File tempFile = new File(Environment.getExternalStorageDirectory()
+ "/Postcard", getPhotoFileName());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
// 初始化控件
private void init() {
img_btn = (ImageButton) findViewById(R.id.imageView1);
btn = (Button) findViewById(R.id.button1);
// 为ImageButton和Button添加监听事件
img_btn.setOnClickListener(this);
btn.setOnClickListener(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,Activity.DEFAULT_KEYS_DIALER);
}
});
}
// 点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageView1:
showDialog();
break;
case R.id.button1:
showDialog();
break;
}
}
// 提示对话框方法
private void showDialog() {
new AlertDialog.Builder(this)
.setTitle("头像设置")
.setPositiveButton("拍照", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
})
.setNegativeButton("相册", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
}).show();
}
@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是否可用
return;
}
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null;
File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹,名称为myimage
//照片的命名,目标文件夹下,以当前时间数字串为名称,即可确保每张照片名称不相同。
String str=null;
Date date=null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");//获取当前时间,进一步转化为字符串
date =new Date();
str=format.format(date);
String fileName = "/sdcard/myImage/"+str+".jpg";
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();
}
if (data!= null) {
Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
System.out.println("fdf================="+data.getDataString());
img_btn.setImageBitmap(cameraBitmap);
System.out.println("成功======"+cameraBitmap.getWidth()+cameraBitmap.getHeight());
}
}
}
}
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
// 将进行剪裁后的图片显示到UI界面上
@SuppressWarnings("deprecation")
private void setPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");
Drawable drawable = new BitmapDrawable(photo);
img_btn.setBackgroundDrawable(drawable);
}
}
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
}
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.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
private ImageButton img_btn;
private Button btn;
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
// 创建一个以当前时间为名称的文件
File tempFile = new File(Environment.getExternalStorageDirectory()
+ "/Postcard", getPhotoFileName());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
// 初始化控件
private void init() {
img_btn = (ImageButton) findViewById(R.id.imageView1);
btn = (Button) findViewById(R.id.button1);
// 为ImageButton和Button添加监听事件
img_btn.setOnClickListener(this);
btn.setOnClickListener(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,Activity.DEFAULT_KEYS_DIALER);
}
});
}
// 点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.imageView1:
showDialog();
break;
case R.id.button1:
showDialog();
break;
}
}
// 提示对话框方法
private void showDialog() {
new AlertDialog.Builder(this)
.setTitle("头像设置")
.setPositiveButton("拍照", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
})
.setNegativeButton("相册", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
}).show();
}
@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是否可用
return;
}
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null;
File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹,名称为myimage
//照片的命名,目标文件夹下,以当前时间数字串为名称,即可确保每张照片名称不相同。
String str=null;
Date date=null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");//获取当前时间,进一步转化为字符串
date =new Date();
str=format.format(date);
String fileName = "/sdcard/myImage/"+str+".jpg";
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();
}
if (data!= null) {
Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
System.out.println("fdf================="+data.getDataString());
img_btn.setImageBitmap(cameraBitmap);
System.out.println("成功======"+cameraBitmap.getWidth()+cameraBitmap.getHeight());
}
}
}
}
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
// 将进行剪裁后的图片显示到UI界面上
@SuppressWarnings("deprecation")
private void setPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");
Drawable drawable = new BitmapDrawable(photo);
img_btn.setBackgroundDrawable(drawable);
}
}
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
}