• Android 打开相册,选择一张图片并返回


    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 + "
    机型: "  + model + "
    厂商: " + make 
    				+ "
    光感: " + iso   + "
    曝光: "  + exposure_time + "
    光圈: " + aperture 
    				+ "
    闪光: " + flash + "
    焦距: "  + focal_len+ "
    高度: " + height 
    				+ "
    宽度: " + width + "
    白平衡: "+  wb;
    		Log.e("readExifInfo", msg);
    
    		AlertDialog.Builder builder = new Builder(MainActivity.this);
    		builder.setTitle("Exif");
    		builder.setMessage(msg);
    		builder.create().show();
    	}
    

      

    吐槽下,如果Eclipse能有VS这么爽就好了~~

  • 相关阅读:
    PHP关于异常说明
    PHP关于微信授权
    python flask使用方法
    scrcpy启动方法
    ADB shell出现error:device offline提示
    使用Spring框架整合Java Mail
    基于Aspectj表达式配置的Spring AOP
    如何安装和使用Maven
    在Linux系统下安装nginx教程
    了解Maven的基本知识
  • 原文地址:https://www.cnblogs.com/listensong/p/4451565.html
Copyright © 2020-2023  润新知