如何将图像传递到 ArrayList<String>在安卓中? [英] How to pass images into ArrayList&lt;String&gt; in Android?

查看:25
本文介绍了如何将图像传递到 ArrayList<String>在安卓中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要设置图像列表,但我无法这样做,因为我不知道如何将字符串数组列表传递给位图.

I have a situation where I need to set a list of images and I am unable to do that cause I am not aware on how to pass a list of string array to a bitmap.

代码:

        private void setImageBit(String item, ImageView imageView){
        path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
        File file = new File(path,"Image 5.jpg");
        String item = file.toString();
        Bitmap bitmap = BitmapFactory.decodeFile(item);
        float i = ((float) imageWidth) / ((float) bitmap.getWidth());
        float imageHeight = i * (bitmap.getHeight());
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
        params.height = (int) imageHeight;
        params.width = (int) imageWidth;
        imageView.setLayoutParams(params);
        imageView.setImageBitmap(bitmap);

    }

我知道上面的代码只会设置指定的Image 5.jpg",但我需要在 MASONRY VIEW 中设置更多的图像.例如,请参见下文.我想在下面描绘的视图中动态设置从相机拍摄的图像.但我无法做到这一点.

I know for a fact that the above code would set only the specified "Image 5.jpg" but I would require more images to be set in a MASONRY VIEW. See below for example. I want to set images taken from a camera in the below depicted view dynamically. But I am unable to achieve that.

我也试过 decodeByteArray() 它会得到数组(我会传递数组中的路径列表)但这似乎不起作用,或者导致将字符串路径转换为字节不会按预期工作.而且我已经尝试了以下

I have also tried decodeByteArray() which would get array(I would pass the list of path in array) but that doesn't seem to work either cause converting string path to byte doesn't work as expected. And also that I have tried the following

   path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2";
   File file = new File(path);
   Bitmap bitmap = BitmapFactory.decodeFile(path);
   imageView.setImageBitmap(bitmap);

但这似乎返回了一组空图像.没有图像的空白页.如果需要,我很乐意为您提供更多代码.任何帮助将不胜感激.提前致谢并随时提出任何说明.

But this seems to return a empty set of images. A blank white page with no image. I would be glad to provide you with more code if required. Any help would be highly appreciated. Thanks in advance and feel free to ask for any clarifications.

推荐答案

你只需要像下面这样用 Bitmap 创建 ArrayList:

You just need to create ArrayList with Bitmap like below:

ArrayList<Bitmap> img_bitmap

现在,只需使用以下函数加载一张一张的图像并将其存储到位图 arrylist 中,如下所示:

and now, just load one by one images using below function and store it into bitmap arrylist like below:

public static Bitmap ImgBitFromFile(String file_name) {

    File imgFile = new File(
            "/data/data/package/app_my_sub_dir/images/" + file_name);
    //System.out.println("Image Exists:::" + imgFile.getAbsolutePath().toString());
    if (imgFile.exists()) {
        // System.gc();
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                .getAbsolutePath());
        //System.out.println("Image Exists:::");

        return myBitmap;
    }
    return null;
}

并存储到位图数组列表中,如下所示:

and store into bitmap arraylist like below:

for (int i = 0; i < c.getCount(); i++) {

            if(c.getString(3)!="") {

                img_bitmap.add(ImgBitFromFile(c.getString(5)));//Image path from Database
                img_name.add(c.getString(3));
                    }
                    c.moveToNext();

                }

最后将一张一张的图片加载到你的网格视图 ImageAdapter 中.

and at the end load one by one images into your grid view ImageAdapter.

这篇关于如何将图像传递到 ArrayList<String>在安卓中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆