Re: [android-developers] Re: Image doesn't appear from sqlite

2010-12-24 Thread Kostya Vasilyev

+0.02 - this code:

public void insertlistNews(String city,*byte[] newsImageUrl*, String

 publishDate, String expiredDate){

[snip]

  + " VALUES ('" + city + "',*'" + imageUrl + "'*,'" + publishDate+
 "','" + expiredDate"');");


appends a byte[] to a String.

This is the same as appending String.valueOf(imageUri), which calls 
byte[].toString, which *does not* return the contents of the array, but 
rather its type marker and address - which does look like "[...@437c1338" 
and so on.


If you want to insert byte[] values into the database, use one of:

- SQLiteStatement and bindBlob(int index, byte[] value)

- DatabaseUtils.InsertHelper and bind(int index, byte[] value)

- ContentValues, put(String key, byte[] value), and SQLiteDatabase.insert()

If you mean to insert the URL as a string, make sure you use a String 
variable for the URL to build your SQL statement.


Perhaps you are trying to do both (your code sample uses two different 
variable names: newsImageUrl and imageUrl), so do each one as described 
above.


-- Kostya

24.12.2010 20:13, DanH ?:

What's varying?  The data won't be read back into the same address,
but into a new byte array, and [...@ is just a byte array at a
certain address.  Dump a few bytes of the arrays (eg, first and last
10) so you can compare their contents.

On Dec 22, 4:46 pm, Amiral<4mi...@gmail.com>  wrote:

Hi all.

I would like to save an image from Internet to database, I've tried to
convert images into bytearray and store them in a database.

Here my code:

this is to created table:
myDB.execSQL("CREATE TABLE IF NOT EXISTS News (Title VARCHAR(20),
imageUrl BLOB, publishDate TIMESTAMP, expiredDate TIMESTAMP);");

and this is my Method to Insert table:

public void insertlistNews(String city,byte[] newsImageUrl, String
publishDate, String expiredDate){
try {
  myDB.execSQL("INSERT INTO News
(title,imageUrl,publishDate,expiredDate)"
 + " VALUES ('" + city + "','" + imageUrl + "','" + publishDate+
"','" + expiredDate"');");
 Log.i("Android", "Table created");} catch (Exception e) {

 Log.i("Android", "No Table created" + e.getMessage());
 }
 //for check bytearray in database
 Log.i("Android", "Image saved in database: "+ urlFrontImage);

}

The last my method for getNews from db:

public ArrayList getAllNews() {
  ArrayList news = new ArrayList();
  Cursor c = myDB.rawQuery("SELECT
title,imageUrl,publishDate,expiredDate
 FROM News WHERE publishedDate<= DATETIME() and expiredDate>=
DATETIME() ORDER BY publishedDate DESC;", null);
  if (c != null) {
   c.moveToFirst();
   int count = 0;
   while (c.moveToNext()) {
if (count == 0)
   c.moveToFirst();
String title = c.getString(0);
byte[] image = c.getBlob(1);
String publishDate = c.getString(2);
String expiredDate = c.getString(3);

NewsItem nl = new NewsItem(title, image, publishDate, expiredDate);

Log.i("Android", "Image get from db: "+image);
news.add(nl);
count++;
   }
  }
return news;

}

when I run it the newsimage not appear and the Log like this:

12-22 00:11:49.362: INFO/Android(307): Image saved in database:
[...@437cbdb8
12-22 00:11:51.052: INFO/Android(307): Image saved in database:
[...@437db710
12-22 00:11:51.862: INFO/Android(307): Image saved in database:
[...@437c1338

12-22 00:12:07.962: INFO/Android(307): Image get from db: [...@4382d370
12-22 00:12:07.982: INFO/Android(307): Image get from db db:
[...@4382d9c0
12-22 00:12:07.992: INFO/Android(307): Image get from db: [...@4382e0e0

why results can vary between insert and getNews?

--
Best Regards

=
Amiral



--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: Image doesn't appear from sqlite

2010-12-24 Thread DanH
What's varying?  The data won't be read back into the same address,
but into a new byte array, and [...@ is just a byte array at a
certain address.  Dump a few bytes of the arrays (eg, first and last
10) so you can compare their contents.

On Dec 22, 4:46 pm, Amiral <4mi...@gmail.com> wrote:
> Hi all.
>
> I would like to save an image from Internet to database, I've tried to
> convert images into bytearray and store them in a database.
>
> Here my code:
>
> this is to created table:
> myDB.execSQL("CREATE TABLE IF NOT EXISTS News (Title VARCHAR(20),
> imageUrl BLOB, publishDate TIMESTAMP, expiredDate TIMESTAMP);");
>
> and this is my Method to Insert table:
>
> public void insertlistNews(String city,byte[] newsImageUrl, String
> publishDate, String expiredDate){
> try {
>  myDB.execSQL("INSERT INTO News
> (title,imageUrl,publishDate,expiredDate)"
>         + " VALUES ('" + city + "','" + imageUrl + "','" + publishDate+
> "','" + expiredDate"');");
>         Log.i("Android", "Table created");} catch (Exception e) {
>
>         Log.i("Android", "No Table created" + e.getMessage());
>         }
>         //for check bytearray in database
>         Log.i("Android", "Image saved in database: "+ urlFrontImage);
>
> }
>
> The last my method for getNews from db:
>
> public ArrayList getAllNews() {
>  ArrayList news = new ArrayList();
>  Cursor c = myDB.rawQuery("SELECT
> title,imageUrl,publishDate,expiredDate
>         FROM News WHERE publishedDate <= DATETIME() and expiredDate >=
> DATETIME() ORDER BY publishedDate DESC;", null);
>  if (c != null) {
>   c.moveToFirst();
>   int count = 0;
>   while (c.moveToNext()) {
>    if (count == 0)
>       c.moveToFirst();
>    String title = c.getString(0);
>    byte[] image = c.getBlob(1);
>    String publishDate = c.getString(2);
>    String expiredDate = c.getString(3);
>
>    NewsItem nl = new NewsItem(title, image, publishDate, expiredDate);
>
>    Log.i("Android", "Image get from db: "+image);
>    news.add(nl);
>    count++;
>   }
>  }
> return news;
>
> }
>
> when I run it the newsimage not appear and the Log like this:
>
> 12-22 00:11:49.362: INFO/Android(307): Image saved in database:
> [...@437cbdb8
> 12-22 00:11:51.052: INFO/Android(307): Image saved in database:
> [...@437db710
> 12-22 00:11:51.862: INFO/Android(307): Image saved in database:
> [...@437c1338
>
> 12-22 00:12:07.962: INFO/Android(307): Image get from db: [...@4382d370
> 12-22 00:12:07.982: INFO/Android(307): Image get from db db:
> [...@4382d9c0
> 12-22 00:12:07.992: INFO/Android(307): Image get from db: [...@4382e0e0
>
> why results can vary between insert and getNews?
>
> --
> Best Regards
>
> =
> Amiral

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en