Ahmed Mourad Ahmed Mourad - 2 months ago 14
Android Question

Setting a Parcelable as an ImageView image

I've been using this library https://android-arsenal.com/details/1/4072 to get images from gallary and camera to set as a profile picture.

TextView chooseImageTextView = (TextView) findViewById(R.id.signUp_uploadPicture_textView);
chooseImageTextView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
ImagePicker.create((Activity) SignUpActivity.this)
.folderMode(true)
.folderTitle("Select image folder")
.imageTitle("Select image")
.single()
.showCamera(true)
.imageDirectory("Camera - Bond Messenger")
.start(REQUEST_CODE_PICKER);
}
}
);


and it's been working pretty fine but the result image comes as a Parcelable ArrayList of the type Image imported from library and so comes the problem here

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICKER && resultCode == RESULT_OK && data != null) {

ArrayList<Image> images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);

profilePictureCircleImageView.setImageBitmap(images.get(0));
}
}


I get an error trying to set the result Image as in Image for the profilePictureCircleImageView

so i've tried casting it to Bitmap, BitmapDrawable and Drawable but none worked

so any help please? thank you

Answer

A quick digging into the source code of your provided library reveals that the Image class is not from the android package android.media.Image.

See Image.class from library on github

So since the class holds the path of the image you can do the following.

Bitmap bmImg = BitmapFactory.decodeFile(images.get(0).getPath());
profilePictureCircleImageView.setImageBitmap(bmImg);