java如何打开图库来选择图像?
我正在寻找一种方法打开onClick画廊来挑选一张照片。 我想在图片列表视图中使用本地“上传”的图片。 有人能帮我解释一下怎么做吗。我是Android开发的初学者
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
# 1 楼答案
以下是我在应用程序中使用的代码
在onActivity result方法中