Abhishek Karande
Abhishek Karande

Reputation: 387

ListView with Images save in SQLite

Im Developing an Application where the ListView with Images will be displayed the text data and Images are downloaded from the services. I want to save the data in SQLite also I want to save the thumbnail Images my code is

public class MainActivity extends Activity {
    // TAG for LogCat
public static final String TAG = "SQLiteDebug";
// list view control
    private ListView mListUserInfo; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

    // load list view control
    mListUserInfo = (ListView)findViewById(R.id.listUsers);
    // create data creator
    UserInfoCreator creator = new UserInfoCreator(this);
    // open connection
    creator.open();
    // insert data
    creator.insert();     
    // add data to list view through adapter
    mListUserInfo.setAdapter(new UserInfoAdapter(this, creator.queryAll()));//,thumbURLStrings));        
    // close connection
    creator.close();

}
}

Data inserted in

public class UserInfoCreator {


    static String [] thumbURLStrings = null;
    // db adapter
    private UserDbAdapter mDbAdapter;

    /*
     * constructor
     */
    public UserInfoCreator(Context c) {
            mDbAdapter = new UserDbAdapter(c);
    }

    /*
     * open DBAdapter connection
     */
    public void open() {
            mDbAdapter.open();
    }

    /*
     * insert random data
     */
    public void insert() {
    mDbAdapter.insertUser("Toteninsel 100. Fall", 1, "André Marx");
    mDbAdapter.insertUser("Geisterbucht 150. Fall", 2, "Astrid Vollenbruch");
    mDbAdapter.insertUser("Feuermond 125. Fall", 3, "André Marx");
    mDbAdapter.insertUser("Top Secret Edition", 4, "Peter Lerangis");
    mDbAdapter.insertUser("Der dreiTag", 5, "Hendrik Buchna");
    mDbAdapter.insertUser("Die blutenden Bilder", 6, "Kari Erlhoff");

        String xml = XMLfunction.getXML("http://ddfzentrale.europa-websites.de/Zentrale_php/XML/Buecher.xml");
        Document doc = XMLfunction.XMLfromString(xml);

        NodeList nodes = doc.getElementsByTagName("book");
        String [] titleStrings = new String[nodes.getLength()];
        String [] authorStrings = new String[nodes.getLength()];

        Log.d("nodeLength","len: "+nodes.getLength());
        for(int i=0;i < nodes.getLength();i++){

            Element eleBook = (Element)nodes.item(i);
            Log.d("Node_TagName", eleBook.getTagName());
            NodeList titleNode = eleBook.getElementsByTagName("Title");
            Element TitleEle = (Element) titleNode.item(0);
            Log.i("Title", "Title - "+TitleEle.getFirstChild().getNodeValue());
            titleStrings[i]= TitleEle.getFirstChild().getNodeValue();
            NodeList AuthorFName1Node = eleBook.getElementsByTagName("AutorFName1");
            Element AuthorFName1Ele = (Element) AuthorFName1Node.item(0);
            Log.i("AuthorFName1","AuthorFName1 - "+AuthorFName1Ele.getFirstChild().getNodeValue());
            NodeList AuthorLName1Node = eleBook.getElementsByTagName("AutorLName1");
            Element AuthorLName1Ele = (Element) AuthorLName1Node.item(0);
            Log.i("AuthorLName1","AuthorLName1 - "+AuthorLName1Ele.getFirstChild().getNodeValue());
            authorStrings[i]=AuthorFName1Ele.getFirstChild().getNodeValue()+" "+AuthorLName1Ele.getFirstChild().getNodeValue();

           mDbAdapter.insertUser(titleStrings[i], i, authorStrings[i]);


        }
     }

    /*
     * query all user info from db
     */
    public List<UserInfo> queryAll() {
            return mDbAdapter.fetchAllUsers();
    }

    /*
     * close connection
     */
    public void close() {
            mDbAdapter.close();
    }

}

Im parsing and inserting data in same class But with the Text I want to display and show thumbnail Image to display thumbnail with the images I used tutorial https://github.com/thest1/LazyList Please anyone have an Idea about how to store the Images in SQLite Please help me

Upvotes: 0

Views: 2587

Answers (2)

Venky
Venky

Reputation: 11107

Check this Code for Inserting images into Database downloading from Server :

Download Image from Server store it as BLOB and Retrieve it back.

Upvotes: 1

Amit Chintawar
Amit Chintawar

Reputation: 20688

It is not advisable to store images in SQLite database. But still it is possible to to so. You can convert the image to a byte array and store it as a BLOB in database. I hope it helps!

Upvotes: 0

Related Questions