我正在尝试创建一个图像选择器,用户将在其中选择一个图像,并将一个图像ImageView附加到一个linear layout图像作为其图像资源。这是我的代码,我不知道出了什么问题,但没有ImageView附加到 LinearLayout
private void pickImage() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode != RESULT_OK) {
return;
} else if(resultCode == 1) {
final Bundle extras = data.getExtras();
if (extras != null) {
//IMAGE SELECTED
Bitmap imageSelected = extras.getParcelable("data");
final View listingImage = getLayoutInflater().inflate(R.layout.image_listing, null);
Button remove = (Button)listingImage.findViewById(R.id.removeImage);
ImageView imageView = (ImageView)listingImage.findViewById(R.id.imageView);
//SET IMAGE OF IMAGEVIEW TO SELECTED IMAGE
imageView.setImageBitmap(imageSelected);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout.removeView(listingImage);
}
});
//ADD IMAGEVIEW TO LINEARLAYOUT
linearLayout.addView(listingImage, 0);
}
}
}
pickImage()从按钮调用。
心有法竹
相关分类