Android SimpleAdapter 添加图像到列表视图

发布于 2024-12-12 06:13:20 字数 1748 浏览 0 评论 0原文

我在使用简单适配器将图像设置为列表视图时遇到一些问题。我使用 sqlite 数据库中的 ID 从 sdcard 获取图像,因此 sqlite 语句正在工作,图像位于 sdcard 上,但如果我尝试在列表视图中设置它们,它会给我一个输出:

resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40566318

在 LogCat 上。这是我用来执行此操作的代码:

    String sqlite2 = "SELECT objectId FROM collectionmedias WHERE collectionId="+collId+" AND mediaType="+fronImage;
             Cursor bg = userDbHelper.executeSQLQuery(sqlite2);
             if (bg.getCount() == 0) {
                 Log.i("", "No Image file");
                 bg.close();
             } else if (bg.getCount() > 0) {
                 for (bg.move(0); bg.moveToNext(); bg.isAfterLast()) {

                    objectId = Integer.parseInt(bg.getString(bg.getColumnIndex("objectId")));
                    Log.i("","objectId : "+objectId);
                     path = Environment.getExternalStorageDirectory()
                             + "/.MyApp/MediaCollection/" + objectId + ".png";
                     Log.v("","path: "+path);

                 }
             }

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inTempStorage = new byte[2*1024];

            Bitmap ops = BitmapFactory.decodeFile(path, options);



             hm = new HashMap<String, Object>();
                hm.put(IMAGE, ops);
                hm.put(TITLE, text);
                hm.put(CARDS_COUNT, cardsCount +" MyApp");
                items.add(hm);
        }

        final SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
                new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
        listView.setAdapter(adapter);

I have a little issue with setting an image to a list view using simple adapter. I'm getting the image from sdcard using ID from sqlite database, so the sqlite statement is working, the images are on sdcard,but if I try to set them in my listview, it's giving me an output :

resolveUri failed on bad bitmap uri: android.graphics.Bitmap@40566318

on LogCat. Here is the code I'm using to do it :

    String sqlite2 = "SELECT objectId FROM collectionmedias WHERE collectionId="+collId+" AND mediaType="+fronImage;
             Cursor bg = userDbHelper.executeSQLQuery(sqlite2);
             if (bg.getCount() == 0) {
                 Log.i("", "No Image file");
                 bg.close();
             } else if (bg.getCount() > 0) {
                 for (bg.move(0); bg.moveToNext(); bg.isAfterLast()) {

                    objectId = Integer.parseInt(bg.getString(bg.getColumnIndex("objectId")));
                    Log.i("","objectId : "+objectId);
                     path = Environment.getExternalStorageDirectory()
                             + "/.MyApp/MediaCollection/" + objectId + ".png";
                     Log.v("","path: "+path);

                 }
             }

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inTempStorage = new byte[2*1024];

            Bitmap ops = BitmapFactory.decodeFile(path, options);



             hm = new HashMap<String, Object>();
                hm.put(IMAGE, ops);
                hm.put(TITLE, text);
                hm.put(CARDS_COUNT, cardsCount +" MyApp");
                items.add(hm);
        }

        final SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
                new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
        listView.setAdapter(adapter);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2024-12-19 06:13:20

我使用以下方法修复了它: path = Environment.getExternalStorageDirectory() + "/MyApp/MediaCollection/" + objectId + ".png";

I fixed it using this : path = Environment.getExternalStorageDirectory() + "/MyApp/MediaCollection/" + objectId + ".png";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文