Android 程序打开相册读取一张图片

开启相册,回调一张图片

开始还要设置读写权限

打开相册

Intent intent=new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,200);

设置返回值

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (( requestCode == REQUEST_IMAGE_SELECT) && resultCode == RESULT_OK) {
            String imgPath;
            if (requestCode == REQUEST_IMAGE_CAPTURE) {
                imgPath = fileUri.getPath();
            } else {
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = MainActivity.this.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgPath = cursor.getString(columnIndex);
                cursor.close();
            }
			// 读取到的图片
            bmp = BitmapFactory.decodeFile(imgPath);
        super.onActivityResult(requestCode, resultCode, data);
    }

你可能感兴趣的:(android,晋级之路)