本文实例为大家分享了AndroID打开相机和相册具体代码,供大家参考,具体内容如下
打开相机
/** * 选择相机 */ private voID showCamera() { // 跳转到系统照相机 Intent cameraIntent = new Intent(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(); } }
打开相册
Intent albumIntent = new Intent(Intent.ACTION_PICK,null); albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); startActivityForResult(albumIntent,REQUEST_ALBUM_OK);
onActivityResult
@OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); switch (requestCode) { case REQUEST_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; case REQUEST_ALBUM_OK: Log.d(TAG,"onActivityResult:相册 " + data.getData().toString()); ContentResolver resolver = getContentResolver(); try { inputStream inputStream = resolver.openinputStream(data.getData()); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); mIvdispaly.setimageBitmap(bitmap); } catch (fileNotFoundException e) { e.printstacktrace(); } // Cursor query = resolver.query(data.getData(),null,null);//// String str = 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(new file(str)).centerCrop().resize(OtherUtils.dip2px(this,100))// .error(R.mipmap.pictures_no).into(mIvdispaly); break; } }
OtherUtils
import androID.content.Context;import androID.os.Environment;import java.io.file;import java.util.Date;/** * Created by 刘楠 on 2016/7/20 0020.22:37 */public class OtherUtils { public static int dip2px(Context context,float dpValue) { final float scale = context.getResources().getdisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int px2dip(Context context,float pxValue) { final float scale = context.getResources().getdisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 获取拍照相片存储文件 * @param context * @return */ public static file createfile(Context context){ file file; if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String timeStamp = String.valueOf(new Date().getTime()); file = new file(Environment.getExternalStorageDirectory() + file.separator + timeStamp+".jpg"); }else{ file cacheDir = context.getCacheDir(); String timeStamp = String.valueOf(new Date().getTime()); file = new file(cacheDir,timeStamp+".jpg"); } return file; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android打开相机和相册实例代码全部内容,希望文章能够帮你解决Android打开相机和相册实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)