Android 打开相册,选择一张图片并返回
2015-04-23 19:38
288 查看
Android 打开相册,选择一张图片并返回
一 打开相册
private static final int ALBUM_OK = 0;
二 显示图片与图片信息
打开相册使用的是
getImagePath(Uri) 用来获取图片的资源路径:
readExifInfo函数(仅给了参考,这里用log来显示exif):
吐槽下,如果Eclipse能有VS这么爽就好了~~
一 打开相册
private static final int ALBUM_OK = 0;
Intent albumIntent = new Intent(Intent.ACTION_PICK, null); albumIntent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(albumIntent, ALBUM_OK);
二 显示图片与图片信息
打开相册使用的是
startActivityForResult(albumIntent, ALBUM_OK); 所以需要一个 onActivityResult(int requestCode, int resultCode, Intent data) 来获取返回结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (ALBUM_OK == requestCode) { Toast.makeText(this, "OK now", Toast.LENGTH_SHORT).show(); Bitmap bitmap; Log.e("MainActivity ", " onActivityResult "); ContentResolver cr = this.getContentResolver(); Uri uri = data.getData(); try { bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); Log.e("MainActivity ", " onActivityResult " + data.getData().toString());//此处用Log.e,仅是为了查看红色Log方便 imageview.setImageBitmap(bitmap); readExifInfo(getImagePath(uri));//这是用来读取图片的exif } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
getImagePath(Uri) 用来获取图片的资源路径:
private String getImagePath(Uri uri) { if (null == uri) { Log.e("getImagePath", "uri return null"); return null; } Log.e("getImagePath", uri.toString()); String path = null; final String scheme = uri.getScheme(); if (null == scheme) { path = uri.getPath(); } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { path = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(uri, proj, null, null, null); int nPhotoColumn = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if (null != cursor) { cursor.moveToFirst(); path = cursor.getString(nPhotoColumn); } cursor.close(); } return path; }
readExifInfo函数(仅给了参考,这里用log来显示exif):
private void readExifInfo(String path) throws IOException { ExifInterface exif = new ExifInterface(path); String iso = exif.getAttribute(ExifInterface.TAG_ISO); String make = exif.getAttribute(ExifInterface.TAG_MAKE); String model = exif.getAttribute(ExifInterface.TAG_MODEL); String flash = exif.getAttribute(ExifInterface.TAG_FLASH); String time = exif.getAttribute(ExifInterface.TAG_DATETIME); String wb = exif.getAttribute(ExifInterface.TAG_WHITE_BALANCE); String aperture = exif.getAttribute(ExifInterface.TAG_APERTURE); String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH); String height = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH); String focal_len = exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH); String exposure_time = exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME); String msg = "时间: "+ time + "\n机型: " + model + "\n厂商: " + make + "\n光感: " + iso + "\n曝光: " + exposure_time + "\n光圈: " + aperture + "\n闪光: " + flash + "\n焦距: " + focal_len+ "\n高度: " + height + "\n宽度: " + width + "\n白平衡: "+ wb; Log.e("readExifInfo", msg); AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setTitle("Exif"); builder.setMessage(msg); builder.create().show(); }
吐槽下,如果Eclipse能有VS这么爽就好了~~
相关文章推荐
- 208_打开相册选择图片并返回
- Android实现选择相册图片和拍照,并手动截图返回图片路径和Bitmap对象
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android 根据URI获取图片(从相册选择图片后返回)
- Android打开图库,选择一张图片
- android开发 怎么在应用中调取图片库 选择一张图片 返回自己的应用中
- Android 打开相册选择单张图片实现代码
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- 【Android图像处理】从相册选择一张图片并分享
- android 通过Intent打开相册并获取选择的图片
- android 开启本地相册选择图片并返回显示
- android从系统相册中选择一张图片
- Android在选择系统相册图片时,返回的图片地址不同导致程序崩溃!!!
- android 拍照+从手机相册选择返回图片到imagview
- android 开启本地相册选择图片并返回显示
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android-打开相册选择单张图片
- android 开启本地相册选择图片并返回显示
- Android 从相册和拍照选择图片
- Android从相机、相册选择图片并裁剪