简体   繁体   中英

Adding images to ListView items and to the details

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.

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

That depends where the image is comming from. Try this or this ,however i'm also having a problem in converting the images from string to drawable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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