将Image中的数据从服务器保存到SQLite数据库,然后在Android的列表视图中显示

时间:2013-11-05 17:56:13

标签: android sqlite http url xml-parsing

我阅读了很多博客和教程,但我没有得到正确答案。我正在研究我的要求是我有一个网址,我以xml的形式获取数据。在Async任务中我正在成功解析数据。然后我必须将数据保存到SQLite数据库然后我将再次从数据库中获取数据并在屏幕上显示它。但是显示空白图像所以我遇到了问题。进行XML解析 -

XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(url); 
//getting XML from URL
    Document doc = parser.getDomElement(xml); 
//getting DOM element
    NodeList nl = doc.getElementsByTagName(PRODUCT);
    for (int i = 0; i < nl.getLength(); i++) {
      Element e = (Element) nl.item(i);
      String name=parser.getValue(e, PRODUCT_NAME);
      String image_url=parser.getValue(e, PRODUCT_IMAGE);
//Changing image to byte[] type because here I am getting image url
       byte[] logoImage = getLogoImage(image_url);
//Inserting data in database
      mySQLiteAdapter.insert_img_Info(name, logoImage);

     }
// Changing image to byte[] using url
    public byte[] getLogoImage(String url) {
     try {
 URL imageUrl = new URL(url);
 URLConnection ucon = imageUrl.openConnection();
 InputStream is = ucon.getInputStream();
     BufferedInputStream bis = new BufferedInputStream(is);
 ByteArrayBuffer baf = new ByteArrayBuffer(500);
 int current = 0;
     while ((current = bis.read()) != -1) {
   baf.append((byte) current);
 }
 photo = baf.toByteArray();
     } catch (Exception e) {
         Log.d("ImageManager", "Error: " + e.toString());
 }
 return photo;
 }
/* Here data is also inserting successfully in SQLite db.But when I am fetching image from database and setting image on ImageView It's not showing any thing*/
    mySQLiteAdapter = new DataBaseHelper(this);
//fetching data to cursor
    Cursor c1 = mySQLiteAdapter.read_img_info();
    if (c1 != null) {
      if (c1.moveToNext()) {
        do {
       name=c1.getString(c1.getColumnIndex("NAME"));
           Getting Image from cursor
       img=c1.getBlob(c1.getColumnIndex("MYIMG"));
            Log.i(TAG, "Image.length="+img.length);
//Here I am getting image length for all images successfully
    } while (c1.moveToNext());
}
//Here in bt it's coming null 
//converting data to Bitmap   
    Bitmap bt = BitmapFactory.decodeByteArray(img, 0, img.length);
    ImageView img1 = (ImageView) findViewById(R.id.imgPhoto);
    img1.setImageBitmap(bt);
    }

//My DatabaseHelper class having code
     public static final String TB_MYIMAGE = "MyImage";
     for c creating table
      private static final String CREATE_DEMO = "CREATE TABLE IF NOT EXISTS  "
    + TB_MYIMAGE + "(NAME VARCHAR(20),MYIMG BLOB NOT NULL);";

// insert record into table
     public void insert_img_Info(String name,byte[] logoImage) {
     myDataBase.execSQL("INSERT INTO "+"'"+TB_MYIMAGE+"'"+ " (NAME,MYIMG) Values
                                         ('" + name + "','"+logoImage+"');");
// get data from table
     public Cursor read_img_info() {
     Cursor c = myDataBase.rawQuery("SELECT NAME,MYIMG FROM MyImage",null);
     if (c != null) {
     return c;
     } else {
  return null;
  }
     }

所以请任何人帮助我。我正在为基于离线场景的应用程序执行此操作。

0 个答案:

没有答案