Unable to convert BLOB to String using Loadermanager in android

I have a table with following structure.

public static final String TABLE_NAME = "contact"; public static final String ID = "_id"; public static final String DEVICE_ID = "device_id"; public static final String NAME = "name"; public static final String CONTACT = "contact"; public static final String IMAGE = "image"; private static final int VERSION = 1; private static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + "( " + ID + " INTEGER PRIMARY KEY , " + DEVICE_ID + " TEXT NOT NULL , " + NAME + " TEXT NOT NULL , " + CONTACT + " TEXT NOT NULL ," + IMAGE + " BLOB " + " ) ";

I have stored images in contact table in BLOB form . Now i have to retrieve device_id,name , image from table and attach the result to view .

I have used Loadermanager for this .

public class FriendList extends Activity implements OnItemClickListener, LoaderManager.LoaderCallbacks { ListView friendList; SimpleCursorAdapter simpleCursorAdapter; CursorLoader cursorLoader; LoaderManager.LoaderCallbacks loaderCallback; static final int CONTACT_LOADER_ID = 77; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.friend_list); friendList = (ListView) findViewById(R.id.lvFriendList); String[] uiBindFrom = new String[] { MyContacts.NAME, MyContacts.DEVICE_ID ,MyContacts.IMAGE}; int[] idBindTo = { R.id.tvFriendName, R.id.tvDeviceID ,R.id.ivContactProfile}; loaderCallback = this; simpleCursorAdapter = new SimpleCursorAdapter(FriendList.this, R.layout.custom_friend_list, null, uiBindFrom, idBindTo, 0); friendList.setAdapter(simpleCursorAdapter); friendList.setOnItemClickListener(this); getLoaderManager().initLoader(CONTACT_LOADER_ID, new Bundle(), loaderCallback); }

Here R.id.ivContactProfile is id of ImageView .

@Override public Loader onCreateLoader(int id, Bundle args) { String[] projectionFields = new String[] { MyContacts.ID, MyContacts.NAME, MyContacts.DEVICE_ID,MyContacts.IMAGE }; cursorLoader = new CursorLoader(FriendList.this, MyContacts.CONTENT_URI, projectionFields, null, null, null); return cursorLoader; } @Override public void onLoadFinished(Loader loader, Cursor cursor) { switch (loader.getId()) { case CONTACT_LOADER_ID: simpleCursorAdapter.swapCursor(cursor); break; } }

Basically i have images save in table in byte[] form , and i have to attach data to the view , but it's returning me an exception like this .

05-21 19:30:05.169: E/AndroidRuntime(22609): android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string

Here is the complete log for this .

05-21 19:30:05.169: E/AndroidRuntime(22609): FATAL EXCEPTION: main 05-21 19:30:05.169: E/AndroidRuntime(22609): android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.CursorWindow.nativeGetString(Native Method) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.CursorWindow.getString(CursorWindow.java:450) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.database.CursorWrapper.getString(CursorWrapper.java:114) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:135) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.AbsListView.obtainView(AbsListView.java:2452) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.ListView.onMeasure(ListView.java:1161) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.measureVertical(LinearLayout.java:670) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 05-21 19:30:05.169: E/AndroidRuntime(22609): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2242) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.View.measure(View.java:12865) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.os.Handler.dispatchMessage(Handler.java:99) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.os.Looper.loop(Looper.java:137) 05-21 19:30:05.169: E/AndroidRuntime(22609): at android.app.ActivityThread.main(ActivityThread.java:4517) 05-21 19:30:05.169: E/AndroidRuntime(22609): at java.lang.reflect.Method.invokeNative(Native Method) 05-21 19:30:05.169: E/AndroidRuntime(22609): at java.lang.reflect.Method.invoke(Method.java:511) 05-21 19:30:05.169: E/AndroidRuntime(22609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 05-21 19:30:05.169: E/AndroidRuntime(22609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 05-21 19:30:05.169: E/AndroidRuntime(22609): at dalvik.system.NativeStart.main(Native Method)

Can anyone tell me why am i getting this exception and how can i resolve this problem ?

Thanks!

-------------Problems Reply------------

I have found solution of my problem . As suggested by @pskink in his comment above , i used ViewBinder . I created a ViewBinder like this .

public class MyViewBinder implements ViewBinder{
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewID = view.getId();
switch(viewID){

case R.id.tvFriendName :
TextView friendName = (TextView) view;
String friend_name;
friend_name = cursor.getString(cursor.getColumnIndex(MyContacts.NAME));
friendName.setText(friend_name);
break;

case R.id.tvDeviceID :
TextView deviceID = (TextView) view;
String device_id = cursor.getString(cursor.getColumnIndex(MyContacts.DEVICE_ID));
deviceID.setText(device_id);
break;

case R.id.ivContactProfile :
ImageView contactProfile = (ImageView) view;
byte[] imageBytes = cursor.getBlob(cursor.getColumnIndex(MyContacts.IMAGE));
if(imageBytes != null ){
// Pic image from database
contactProfile.setImageBitmap(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length));
}else {
// If image not found in database , assign a default image
contactProfile.setBackgroundResource(R.drawable.bubble_a);
}
break;
}
return true;
}

}

And i set ViewBinder for my SimpleCursorAdapter .

simpleCursorAdapter.setViewBinder( new MyViewBinder());

It worked for me properly . Here is the output.

Unable to convert BLOB to String using Loadermanager in android

Category:android Views:1 Time:2018-07-29

Related post

  • Magic strings for converting DateTime to string Using C# 2009-11-11

    I was greeted with a nasty bug today. The task is pretty trivial, all I needed to do is to convert the DateTime object to string in "yyyymmdd" format. The "yyyymmdd" part was stated in the development doc from the external software vendor. So, I conv

  • Cocoa/Objective C: Convert numbers to strings using Japanese unicode characters 2010-09-06

    Converting numbers to strings is no problem, but the string created uses ascii numbers. I am inserting the number in a Japanese sentence. The character itself is correct, say 3, for example, but the width and positioning should be a little different

  • Converting xml to string using C# 2011-05-28

    I have a function as below public string GetXMLAsString(XmlDocument myxml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(myxml); StringWriter sw = new StringWriter(); XmlTextWriter tx = new XmlTextWriter(sw); doc.WriteTo(tx); string str = sw.ToS

  • Blob to String : How to convert Blob to string from StoredProcedure Parameter in PostgreSQL? 2012-02-14

    I have stored procedure (function in Postgres) with type of parameter like this : Params[0] : result = ftBlob // postgresql function = text Params[1] : 1 = ftString Params[2] : 2 = ftInteger Params[3] : 3 = ftInteger my code is like this : procedure

  • Converting Timestamp as String to Date in android 2010-12-07

    I am getting Timestamp value as 1291204449 from server so i extracted the value by implementing handler. Now i want to convert timestamp value to date.(But the problem is in my activity i stored this value in a string variable because handler returni

  • Can't convert Resource to string using TypeConverter 2009-03-23

    Exception: Cannot convert the value in attribute 'Text' to object of type 'System.String'. Unable to cast object of type 'MyApp.Foo' to type 'System.String'. XAML: <Window.Resources> <my:Foo x:Key="foo"></my:Foo> </Window.Resourc

  • How do I convert double to string using only math.h? 2009-09-16

    I am trying to convert a double to a string in a native NT application, i.e. an application that only depends on ntdll.dll. Unfortunately, ntdll's version of vsnprintf does not support %f et al., forcing me to implement the conversion on my own. The

  • iPhone : Unable to convert date from string 2011-11-11

    I am trying to convert string into NSDate using following code. NSString *old = [[NSUserDefaults standardUserDefaults] valueForKey:@"OLD_DATE"]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MM-yyyy"]; NSD

  • Converting masked currency string using Decimal.TryParse 2011-11-15

    I'm having issue converting this string to a decimal. I tried to follow the documentation here with no luck: Decimal.TryParse Method string stringVal = "-(3434343434.00)"; NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousa

  • How to convert integer to string using current culture in .NET? 2011-11-30

    How do i convert an integer to a localized string in .NET? Exempli gratia: Int64 value = 12345678901; value.ToString(); // returns "12345678901", should be "123,4567,8901" value.ToString(CultureInfo.CurrentCulture); // returns "12345678901", should b

  • Unable to convert some date strings to numeric format in Excel (2007). 2013-09-12

    I use an MS Word (2007) merge template to gather data from a software database (Amicus Attorney) then save the resulting document as a plain text file for importing to an Excel spreadsheet. Two fields are dates (formatted "September 1, 2006") and, in

  • unable to convert pdf to text using python script 2010-10-18

    i want to convert all my .pdf files from a specific directory to .txt format using the command pdftotext... but i wanna do this using a python script... my script contains: import glob import os fullPath = os.path.abspath("/home/eth1/Downloads") for

  • Unable to convert object to string c#? 2011-11-24

    I am trying to extract values from AraryList but all i keep getting is System.Object[]... protected void Page_Load(object sender, EventArgs e) { ReadDb readClass = new ReadDb(); ArrayList list = new ArrayList(); list = readClass.ReadFromDb(); string

  • Convert byte[] to String using binary encoding 2011-12-07

    I want to translate each byte from a byte[] into a char, then put those chars on a String. This is the so-called "binary" encoding of some databases. So far, the best I could find is this huge boilerplate: byte[] bytes = ...; char[] chars = new char[

  • Convert blob to string 2013-01-23

    I am working on a project where I have to store a large amount of data into database. For example 1200 - 2000 lines of code in a single column. When I read the information back from database I want to customize those codes. For example one of the lin

  • Unable to convert a SQL statements (uses a HAVING clause and a temp table) to a Rails regular .find query finder. 2010-05-12

    I can't get the following SQL query to work quite right in Rails. It runs, but it fails to do the "HAVING row_number = 1" part, so I'm getting all the records, instead of just the first record from each group. A quick description of the query: it is

  • unable to url from this string using xpath? 2011-04-23

    I want to get the url of the Download hyper link from this link: http://mp3skull.com/mp3/aadat.html This is the string that I have written: //div[1]/[text() = 'Download'] (and this is only for the first object you ll see what I mean). What am I doing

  • Microsoft Access: How to convert number to string using update query? 2011-06-20

    I have an integer field with numbers ranging from 1000 to 99999. I want to convert this number field to a 5-character text field. This means all 4 digit numbers will have 0 added in front when converted to text. Example: 1000 in integer must be conve

  • Parse JSON date format to String using GSON on Android 2011-06-13

    I´m getting a lot of dates from a JSON feed. They look like this: \/Date(1307972400000+0200)\/ I need to parse these dates into hours and minutes using Java. EDIT: This is how far I´ve come: String s = dateString.replaceAll("^/Date\\(" , ""); This gi

  • Crash when parsing protobuf message containing a string using C++ and Android NDK 2011-09-30

    I have two programs, both running on an Android 2.3.4 platform - one is a C++ binary in a daemon form, compiled with cmake, the other is a apk with jni part compiled with ndk-build. I'm using android-ndk-r6. I'm using protobuf messages and local unix

  • Using ffmpeg to convert sound files for use in an android app 2011-11-29

    short: i'm trying to simply play a sound file converted with ffmpeg in my android app, but happen to have problems getting it to work. long: we have an iphone app and an android app doing the same thing, and i have to port the feature playing a sound

  • Unable to pause/forward/backward video using mediacontroller in android 2013-08-18

    Iam unable to pause / forward / backward the video using MediaController and it is happening only in the LG devices. Below is the code which am using and it is working in the rest of all the devices : VideoView video=(VideoView)findViewById(R.id.vide

  • how to parse the array of json string using gson in android 2011-03-17

    { "serviceType":"IMAGE_ANDROID", "parameters":[ [ { "itemId":"it003376", "itemNameInEng":"8th Chennai International Film Festival Photos", "itemSmallImage":"http://122.183.217.134:8080/sivajitv/photos/20101216001017.jpg", "count":"110" }, { "itemId":

  • Convert generic Collection to JSON string using JSONSimple in Java 2012-01-03

    I have a non-typed collection of Entities and want to convert to JSON String, using JSONSimple. I've tried with JSONObject like: Collection entities; //supose its a non-typed Collection returned from service. JSONObject colJSON = new JSONObject(); co

  • Converting Integers to strings in Fortran 2009-08-11

    I have a program in Fortran that saves the results to a file. At the moment I open the file using OPEN (1, FILE = 'Output.TXT') However, I now want to run a loop, and save the results of each iteration to the files 'Output1.TXT', 'Output2.TXT', 'Outp

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.229 (s). 11 q(s)