Android 打开相册,选择一张图片并返回
16lz
2021-01-23
Android 打开相册,选择一张图片并返回
一 打开相册
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这么爽就好了~~
转载于:https://www.cnblogs.com/listensong/p/4451565.html
更多相关文章
- android 加载图片的三种方式
- android中给图片打水印
- android等比例缩放图片
- android 笔记 --- Android-图片透明度处理代码
- Android Retrofit2.0 上传单张图片和多张图片
- android bitmap图片压缩,打时间水印。