3

我阅读了很多博客和教程,但没有得到正确的答案。我正在研究我的要求是我有一个 url,我可以通过它以 xml 的形式获取数据。在异步任务中,我成功地解析了数据。然后我必须将数据保存到 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;
  }
     }

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

4

0 回答 0