简体   繁体   English

Android Gridview不显示资产文件夹中的图像

[英]Android Gridview is not showing images from assets folder

I have a specific problem which has not be answered yet on stackoverflow; 我有一个特定的问题,在stackoverflow上还没有得到解决。 I have images in the assets folder numbered like 0.jpg, 1.jpg, 2.jpg etc. Using a for loop I select three images from the asssets folder and I am trying to add these images to a gridview but the images are not showing. 我在资源文件夹中有编号为0.jpg,1.jpg,2.jpg等的图像。使用for循环,我从资产文件夹中选择了三张图像,我试图将这些图像添加到gridview中,但是这些图像却没有显示。 The activity starts up okay just no images! 活动启动好,只是没有图像!

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        gridView = (GridView) findViewById(R.id.gridview_result);

        // Sets the Tag
        gridView.setTag(GRIDVIEW_TAG);

        /*
         * Adapt the image for the GridView format
         */
        imageAdapter = new ImageGridViewAdapter(getApplicationContext());
        gridView.setAdapter(imageAdapter);

        // Set the orientation to landscape
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // Retrieve 3 images form the database which appear
        // similar
        for (int i = 0; i < 3; i++) {
            // System.out.println(Retrieval.distances[i][0]);
            image = Retrieval.distances[i][0];
            int num = (int) image;
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.append(num);
            String imageNum = sBuilder.toString();
            System.out.println(imageNum);

            String file = imageNum + ".jpg";

            try {
                // get input stream
                InputStream ims = getAssets().open(file);
                Log.i("ERROR_IMS", ims + "");
                // load image as Drawable
                Drawable d = Drawable.createFromStream(ims, file);
                // set image to ImageView
                gridView.setBackground(d);
                Log.i("ERROR_d", d + "");
                Log.i("ERROR_gridview", gridView+"");
            } catch (IOException ex) {
                Log.e("I/O ERROR", "Failed when ...");
            }
        }
    }

I believe the issue is occurring in the try/catch. 我相信问题是在try / catch中发生的。 Any help will be much appreciated! 任何帮助都感激不尽!

You should get all images first and set it to your adapter. 您应该首先获取所有图像并将其设置为适配器。 // set image to ImageView gridView.setBackground(d); //将图片设置为ImageView gridView.setBackground(d); doesn't affect to your grid items view. 不会影响您的网格项目视图。

A good tutorial for it: guide 一个很好的教程: 指南

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

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