如何通过图像转换成ArrayList中<字符串>在Android的? [英] How to pass images into ArrayList<String> in Android?

查看:113
本文介绍了如何通过图像转换成ArrayList中<字符串>在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要设置图像的列表,我不能这样做,因为我不知道如何字符串数组列表传递给一个位图。

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.

code:

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

    }

我知道一个事实,即上述code将只设置指定的图像5.JPG但我需要更多的影像在砌体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.

我也尝试德codeByteArray()这将得到阵列(我将通过在阵列路径的列表),但似乎并没有工作无论是导致将字符串转换路径字节为不正常工作。而且也是我曾尝试以下

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

但这似乎返回一个空集图像。一个空白页面,没有图像。我会很高兴,如果需要,为您提供更多的code。任何帮助将是非常美联社preciated。在此先感谢,并随时要求任何澄清。

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.

推荐答案

您只需要创建的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;
}

和存储为位图ArrayList中像下面:

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中&LT;字符串&GT;在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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