简体   繁体   English

将Parcelable设置为ImageView图像

[英]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. 我一直在使用这个库https://android-arsenal.com/details/1/4072从图库和相机获取图像以设置为个人资料图片。

    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 它一直工作得很好,但是结果图像是从库中导入的Image类型的Parcelable ArrayList,所以这里出现了问题

    @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 我在尝试将结果Image设置为profilePictureCircleImageView的Image时遇到错误

so i've tried casting it to Bitmap, BitmapDrawable and Drawable but none worked 所以我试过将其强制转换为Bitmap,BitmapDrawable和Drawable,但均无效果

so any help please? 有什么帮助吗? thank you 谢谢

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 . 快速浏览提供的库的源代码可发现Image类不是来自android包android.media.Image

See Image.class from library on github 从github上的库中查看Image.class

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);

What is returned is not the actual image, but an Objects that holds the image's path. 返回的不是实际图像,而是一个保存图像路径的对象。 You'll have to load it manually: 您必须手动加载它:

String path = images.get(i).getPath()

Bitmap bitmap = BitmapFactory.decodeFile(path, null);

profilePictureCircleImageView.setImageBitmap(bitmap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM