如果打开图库并选择“图像”并与我的应用共享,如何获取图像的Uri
答案 0 :(得分:0)
打开图库以获取Image Uri
private static final int REQUEST_SELECT_IMAGE = 100;
private void handleOpenGallery() {
Intent intentSelect = new Intent(Intent.ACTION_GET_CONTENT);
intentSelect.setType("image/*");
startActivityForResult(Intent.createChooser(intentSelect, getString(R.string.select_picture)),
REQUEST_SELECT_IMAGE);
}
所选图片的Uri
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
shareImage(uri);
}
}
}
分享图片
private void shareImage(Uri imageUri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to"));
}
答案 1 :(得分:0)
您可以按照Raghu的以下代码打开画廊,
如果您只想打开图库,则可以使用
Intent intentSelect = new Intent(Intent.ACTION_PICK);
如果操作系统版本高于棉花糖(6.0),则添加对读/写权限的检查
如果仍然没有获取uri,则在onActivityResult中获取uri try the getPath method from here
添加检查Android OS版本是否高于Nougat(7.0),如高于Nougat一样,在获取文件uri时,您可能会收到“ FileUriExposedException” refer this link for more info