简体   繁体   English

如何从Image Arraylist设置imageview?

[英]How to set imageview from Image Arraylist?

I am using this library but they did not explain all details like other libraries on the github. 我正在使用库,但他们没有像github上的其他库一样解释所有详细信息。

in onCreate 在onCreate中

ImagePicker.create(this)
            .folderMode(true) // folder mode (false by default)
            .folderTitle("Folder") // folder selection title
            .imageTitle("Tap to select") // image selection title
            .single() // single mode
          // multi mode (default mode)
            .limit(10) // max images can be selected (99 by default)
            .showCamera(true) // show camera or not (true by default)
            .imageDirectory("Camera") // directory name for captured image  ("Camera" folder by default)
             // original selected images, used in multi mode
            .start(12); // start image picker activity with request code

It is working I can see gallery. 我可以看到画廊,它正在工作。

onActivityResult: onActivityResult:

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        ImageView imageview = (ImageView)findViewById(R.id.iv1);
        if (requestCode == 12 && resultCode == RESULT_OK && data != null) {
            ArrayList<Image> images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);
            // do your logic ....
            imageview.setImageBitmap(images);//It is not working, I know it is not bitmap but how to set?
        }
    }

Finally how to set imageview ? 最后如何设置imageview?

You can't set an image view to an arraylist of images, you can only set an image view to have one image as a source. 您不能将图像视图设置为图像的数组列表,而只能将图像视图设置为以一个图像为源。

Also, looks like you are processing your images on the main thread, which is also not a good idea. 另外,看起来您正在主线程上处理图像,这也不是一个好主意。

your images are in the Array, but you have to use them on multiple image views or a custom view that displays multiple images 您的图像在数组中,但是您必须在多个图像视图或显示多个图像的自定义视图中使用它们

private Bitmap getScreenshot(Image image) {

    Image.Plane[] planes = image.getPlanes();
    ByteBuffer buffer = planes[0].getBuffer();

    int pixelStride = planes[0].getPixelStride();
    int rowStride = planes[0].getRowStride();
    int rowPadding = rowStride - pixelStride * mWidth;

    // ??????Bitmap???
    Bitmap bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.RGB_565);
    bitmap.copyPixelsFromBuffer(buffer);
    image.close();

    return bitmap;
}

Image is resource rather than a image but you can use above API to get bitmap by using ByteBuffer. 图像是资源而不是图像,但是您可以使用上述API通过使用ByteBuffer来获取位图。 Pass images.get(position) in getScreenshot(Image image). 在getScreenshot(Image image)中传递images.get(position)。 it'll return bitmap then simply set imageView.setImageBitmap(bitmap) Example link 它会返回位图,然后只需设置imageView.setImageBitmap(bitmap) 示例链接

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

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