Android中打开相机、打开相册进行图片
2018-01-04 00:00
441 查看
这里介绍在Android中实现相机调取、拍照片、获取照片、存储新路径等已经打开相册、选择照片等功能
首先看一下界面,很简单
配置读取内存卡和调用照相头的功能
?
下面是代码的主题
?
首先看一下界面,很简单
配置读取内存卡和调用照相头的功能
?
1 2 3 4 5 6 7 | <!-- 使用网络权限 --> < uses-permission android:name = "android.permission.INTERNET" /> <!-- 写sd卡的权限 --> < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 读sd卡权限 --> < uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" /> < uses-permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | public class TakePhotos extends Activity implements android.view.View.OnClickListener { Button takePhoto; Bitmap photo; String picPath; Button capture; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.activity_photo); takePhoto = (Button) findViewById(R.id.button1); capture = (Button) findViewById(R.id.capture); takePhoto.setOnClickListener( ); capture.setOnClickListener( this ); } @Override public void onClick(View viewid) { switch (viewid.getId()) { case R.id.button1: { // 打开相机 String state = Environment.getExternalStorageState(); // 获取内存卡可用状态 if (state.equals(Environment.MEDIA_MOUNTED)) { // 内存卡状态可用 Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE" ); startActivityForResult(intent, 1 ); } else { // 不可用 Toast.makeText(TakePhotos. this , "内存不可用" ,Toast.LENGTH_LONG) .show(); } break ; } case R.id.capture: { // 打开相册 // 打开本地相册 Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // 设定结果返回 startActivityForResult(i, 2 ); break ; } default : break ; } } @Override protected void onActivityResult( int requestCode, int resultCode,Intent data) { // TODO Auto-generated method stub super .onActivityResult(requestCode,resultCode,data); if (data != null ) { switch (requestCode) { case 1 : // 两种方式 获取拍好的图片 if (data.getData() != null || data.getExtras() != null ) { // 防止没有返回结果 Uri uri = data.getData(); if (uri != null ) { this .photo = BitmapFactory.decodeFile(uri.getPath()); // 拿到图片 } if (photo == null ) { Bundle bundle = data.getExtras(); if (bundle != null ) { photo = (Bitmap) bundle.get( "data" ); FileOutputStream fileOutputStream = null ; try { // 获取 SD 卡根目录 生成图片并 String saveDir = Environment .getExternalStorageDirectory() + "/dhj_Photos" ; // 新建目录 File dir = new File(saveDir); if (!dir.exists()) dir.mkdir(); // 生成文件名 SimpleDateFormat t = new SimpleDateFormat( "yyyyMMddssSSS" ); String filename = "MT" +(t.format( new Date())) + ".jpg" ; // 新建文件 File file = new File(saveDir,filename); // 打开文件输出流 fileOutputStream = new FileOutputStream(file); // 生成图片文件 this .photo.compress(Bitmap.CompressFormat.JPEG, 100 ,fileOutputStream); // 相片的完整路径 this .picPath = file.getPath(); ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap( this .photo); } catch (Exception e) { e.printStackTrace(); } finally { if (fileOutputStream != null ) { try { fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } Toast.makeText(getApplicationContext(), "获取到了" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "找不到图片" , Toast.LENGTH_SHORT).show(); } } } break ; case 2 : { //打开相册并选择照片,这个方式选择单张 // 获取返回的数据,这里是android自定义的Uri地址 Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; // 获取选择照片的数据视图 Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null , null , null ); cursor.moveToFirst(); // 从数据视图中获取已选择图片的路径 int columnIndex = cursor.getColumnIndex(filePathColumn[ 0 ]); String picturePath = cursor.getString(columnIndex); cursor.close(); // 将图片显示到界面上 ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); break ; } default : break ; } } } } |
相关文章推荐
- android打开相册选取图片或打开相机拍照并进行裁剪
- android中打开相机、打开相册进行图片的获取示例
- android中打开相机、打开相册进行图片的获取
- Android打开系统相机、相册,并进行裁剪图片的工具类CameraUtil,以及相册获取图片注意要点。
- Android拍照和从相册获取图片(解决android7.0打开相机崩溃的问题),同时也解决了拍完照后图片方向不正的问题
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- JS 使用 Android + IOS 相册 + 相机 进行自定义头像 + 限制裁剪后图片尺寸 + base64 string 与 图片互相转换
- Android 调用相机、打开相册、裁剪图片
- [Android] 调用相机、打开相册、裁剪图片
- Android拍照和从相册获取图片(解决android7.0打开相机崩溃的问题),同时也解决了拍完照后图片方向不正的问题
- Android基础--调用系统相机,打开相册,展示到界面,出现的一些问题Uri为空,data为null,图片错乱问题
- Android从相册选图或相机拍照后对图片进行裁剪
- android调用系统相机拍照与打开相册选择照片进行截取保存笔记
- Android基础--调用系统相机,打开相册,裁剪图片并展示到界面
- 【Android】使用系统相机或者从相册中获取图片资源并进行适当裁剪
- android实例之——调用android系统相机、系统相册,并对得到的图片进行处理
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android 中打开相册图片 使用相机拍摄并存储照片