将图像添加到 ListView 项目和详细信息

发布于 2025-01-04 06:50:45 字数 1374 浏览 2 评论 0原文

大家好,我一直在关注本教程: http://coenraets.org/blog/android-示例/androidtutorial/ 。我从中得到了我需要的东西,但现在我遇到了一个问题。 基本上,我需要的是能够为每个员工添加一张照片。我想要在创建的列表上显示缩略图,并在详细信息页面上显示全尺寸照片。 我该怎么做?

我尝试在数据库中添加照片标记,

String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                        "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                        "firstName TEXT, " +
                        "lastName TEXT, " +
                        "title TEXT, " +
                        "officePhone TEXT, " +
                        "cellPhone TEXT, " +
                        "email TEXT, " +
                        photo IMAGE," + "managerId INTEGER)";
        db.execSQL(sql);

然后添加 values.put("photo", "@drawable/icon");

然后在EmployeeDetails中我添加了这一行:

photo = (ImageView) findViewById(R.id.image);
photo.setTag(cursor.getString(cursor.getColumnIndex("photo")));

当然我导入了ImageViewprotected ImageView photo; 对于详细信息布局,我添加了:

<ImageView
        android:id="id/image
        android:layout_width="wrap_content"
        android:layout_heigh=wrap_content

询问我是否没有设法让自己足够清楚。 我试图为员工的详细信息部分放一张照片,但我并没有尝试制作缩略图,但事实是,我完成了整个过程,结果没有错误,但当我运行它时,应用程序崩溃了。

Hello everyone i've been following this tutorial: http://coenraets.org/blog/android-samples/androidtutorial/ . I got what i needed from it , but now i have reached a problem.
Basically , what i need is to be able to add a picture for each employee. I want a thumbnail picture on the list created and a full sized photo on the details page.
How can i do this?

I tried to add a Photo marker in the database

String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                        "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                        "firstName TEXT, " +
                        "lastName TEXT, " +
                        "title TEXT, " +
                        "officePhone TEXT, " +
                        "cellPhone TEXT, " +
                        "email TEXT, " +
                        photo IMAGE," + "managerId INTEGER)";
        db.execSQL(sql);

then i added values.put("photo", "@drawable/icon");.

Then in EmployeeDetails i added this line :

photo = (ImageView) findViewById(R.id.image);
photo.setTag(cursor.getString(cursor.getColumnIndex("photo")));

Of course i imported ImageView and protected ImageView photo;
and for the Details layout i added:

<ImageView
        android:id="id/image
        android:layout_width="wrap_content"
        android:layout_heigh=wrap_content

Ask if i didn't managed to make myself clear enough.
I was trying to put a photo for the details section of the employee I wasn't trying to make a thumbnail image yet the thing is , i did the whole thing and it came out without errors but when i run it , the app crashes.

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

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

发布评论

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

评论(1

╄→承喏 2025-01-11 06:50:45

使用 blob 从数据库存储和检索图像,this 会给出一个更好的主意:

您还将资产文件夹路径作为文本存储在数据库中,并使用它从 sql 检索路径:
然后你的sql:

String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "firstName TEXT, " +
                    "lastName TEXT, " +
                    "title TEXT, " +
                    "officePhone TEXT, " +
                    "cellPhone TEXT, " +
                    "email TEXT, " +
                    "photo Text," + 
                    "managerId INTEGER)";
    db.execSQL(sql);

然后你可以通过以下方式从资产文件夹中获取drawable:

Drawable d = Drawable.createFromStream(getAssets().open("path_from_db"), null);
img.setBackgroundResource(d);

using blob storing and retriving image from database, this would give a better idea:

you also store assets folder path in db as text and use it to retrieve path from sql:
then your sql:

String sql = "CREATE TABLE IF NOT EXISTS employee (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "firstName TEXT, " +
                    "lastName TEXT, " +
                    "title TEXT, " +
                    "officePhone TEXT, " +
                    "cellPhone TEXT, " +
                    "email TEXT, " +
                    "photo Text," + 
                    "managerId INTEGER)";
    db.execSQL(sql);

then you can get drawable from assets folder by:

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