cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,TAKE_PHOTO);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
Bitmap bitmap=(Bitmap)bundle.get("data");
photoImageView.setImageBitmap(bitmap);
}
}
/**
* 自定义图片名,获取照片的file
*/
private File createImgFile(){
//确定文件名
String fileName="img_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".jpg";
// File dir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// File dir=Environment.getExternalStorageDirectory();
File dir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
dir=Environment.getExternalStorageDirectory();
}else{
dir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);
}
File tempFile=new File(dir,fileName);
try{
if(tempFile.exists()){
tempFile.delete();
}
tempFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
//获取文件路径
photoPath=tempFile.getAbsolutePath();
return tempFile;
}
//拍照
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//只是加了一个uri作为地址传入
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile=createImgFile();
photoUri=Uri.fromFile(photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
startActivityForResult(intent,TAKE_PHOTO);
}
});
/**
* 压缩图片
*/
private void setImageBitmap(){
//获取imageview的宽和高
int targetWidth=photoImageView.getWidth();
int targetHeight=photoImageView.getHeight();
//根据图片路径,获取bitmap的宽和高
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(photoPath,options);
int photoWidth=options.outWidth;
int photoHeight=options.outHeight;
//获取缩放比例
int inSampleSize=1;
if(photoWidth>targetWidth||photoHeight>targetHeight){
int widthRatio=Math.round((float)photoWidth/targetWidth);
int heightRatio=Math.round((float)photoHeight/targetHeight);
inSampleSize=Math.min(widthRatio,heightRatio);
}
//使用现在的options获取Bitmap
options.inSampleSize=inSampleSize;
options.inJustDecodeBounds=false;
Bitmap bitmap=BitmapFactory.decodeFile(photoPath,options);
photoImageView.setImageBitmap(bitmap);
}
//将图片添加进手机相册
private void galleryAddPic(){
Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(photoUri);
this.sendBroadcast(mediaScanIntent);
}
//从相册获取照片
albumButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent intent=new Intent(Intent.ACTION_PICK);
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,PICK_PHOTO);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
switch (requestCode){
case TAKE_PHOTO:
setImageBitmap();
galleryAddPic();
break;
case PICK_PHOTO:
//data中自带有返回的uri
photoUri=data.getData();
//获取照片路径
String[] filePathColumn={MediaStore.Audio.Media.DATA};
Cursor cursor=getContentResolver().query(photoUri,filePathColumn,null,null,null);
cursor.moveToFirst();
photoPath=cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
cursor.close();
//有了照片路径,之后就是压缩图片,和之前没有什么区别
setImageBitmap();
break;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有