Android打开相机和相册实例代码
本文实例为大家分享了Android打开相机和相册具体代码,供大家参考,具体内容如下
打开相机
/**
*选择相机
*/
privatevoidshowCamera(){
//跳转到系统照相机
IntentcameraIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(getPackageManager())!=null){
//设置系统相机拍照后的输出路径
//创建临时文件
mTmpFile=OtherUtils.createFile(getApplicationContext());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(mTmpFile));
startActivityForResult(cameraIntent,REQUEST_CAMERA);
}else{
Toast.makeText(getApplicationContext(),R.string.msg_no_camera,Toast.LENGTH_SHORT).show();
}
}
打开相册
IntentalbumIntent=newIntent(Intent.ACTION_PICK,null); albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(albumIntent,REQUEST_ALBUM_OK);
onActivityResult
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
super.onActivityResult(requestCode,resultCode,data);
switch(requestCode){
caseREQUEST_CAMERA:
//请求相机
if(mTmpFile!=null){
Log.d(TAG,"onActivityResult:请求相机:"+mTmpFile.getAbsolutePath());
Picasso.with(this).load(mTmpFile).centerCrop().resize(OtherUtils.dip2px(this,100),OtherUtils.dip2px(this,100))
.error(R.mipmap.pictures_no).into(mIvDispaly);
}
break;
caseREQUEST_ALBUM_OK:
Log.d(TAG,"onActivityResult:相册"+data.getData().toString());
ContentResolverresolver=getContentResolver();
try{
InputStreaminputStream=resolver.openInputStream(data.getData());
Bitmapbitmap=BitmapFactory.decodeStream(inputStream);
mIvDispaly.setImageBitmap(bitmap);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
//Cursorquery=resolver.query(data.getData(),null,null,null,null);
//
//Stringstr=null;
//while(query.moveToNext()){
//Log.d(TAG,"onActivityResult:数量"+query.getCount());
//str=query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
//Log.d(TAG,"onActivityResult:列名"+query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA)));
//}
//query.close();
//Picasso.with(this).load(newFile(str)).centerCrop().resize(OtherUtils.dip2px(this,100),OtherUtils.dip2px(this,100))
//.error(R.mipmap.pictures_no).into(mIvDispaly);
break;
}
}
OtherUtils
importandroid.content.Context;
importandroid.os.Environment;
importjava.io.File;
importjava.util.Date;
/**
*Createdby刘楠on2016/7/200020.22:37
*/
publicclassOtherUtils{
publicstaticintdip2px(Contextcontext,floatdpValue){
finalfloatscale=context.getResources().getDisplayMetrics().density;
return(int)(dpValue*scale+0.5f);
}
publicstaticintpx2dip(Contextcontext,floatpxValue){
finalfloatscale=context.getResources().getDisplayMetrics().density;
return(int)(pxValue/scale+0.5f);
}
/**
*获取拍照相片存储文件
*@paramcontext
*@return
*/
publicstaticFilecreateFile(Contextcontext){
Filefile;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
StringtimeStamp=String.valueOf(newDate().getTime());
file=newFile(Environment.getExternalStorageDirectory()+
File.separator+timeStamp+".jpg");
}else{
FilecacheDir=context.getCacheDir();
StringtimeStamp=String.valueOf(newDate().getTime());
file=newFile(cacheDir,timeStamp+".jpg");
}
returnfile;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短