尝试在 a 中加载一些联系人照片时,我在某些设备上遇到以下错误CursorAdapter
:
java.lang.RuntimeException: An unexpected exception occurred
at com.squareup.picasso.Request$1.void run()(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "AND": syntax error: , while compiling: SELECT DISTINCT data15 FROM view_data_restricted data WHERE ( AND _id=photo_id1 AND _id=photo_id AND contact_id=?)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
at android.content.ContentResolver.query(ContentResolver.java:321)
at android.provider.ContactsContract$Contacts.openContactPhotoInputStream(ContactsContract.java:1968)
at com.squareup.picasso.Utils$ContactPhotoStreamIcs.java.io.InputStream get(android.content.ContentResolver,android.net.Uri)(Unknown Source)
at com.squareup.picasso.Utils.boolean parseResponseSourceHeader(java.lang.String)(Unknown Source)
java.io.File createDefaultCacheDir(android.content.Context)
java.io.InputStream getContactPhotoStream(android.content.ContentResolver,android.net.Uri)
at com.squareup.picasso.Picasso.android.graphics.Bitmap loadFromType(com.squareup.picasso.Request)(Unknown Source)
at com.squareup.picasso.Picasso.android.graphics.Bitmap resolveRequest(com.squareup.picasso.Request)(Unknown Source)
at com.squareup.picasso.Picasso.void run(com.squareup.picasso.Request)(Unknown Source)
at com.squareup.picasso.Request.void run()(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
at com.squareup.picasso.Utils$PicassoThread.void run()(Unknown Source)
用于获取照片流的 SQL 查询无效。
这是我加载图像的方式:
查询ContactsContract
:
public static final String[] PROJECTION = { Contacts._ID, Contacts.LOOKUP_KEY, Contacts.DISPLAY_NAME_PRIMARY,
Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.CommonDataKinds.Email.DATA };
public static final String SELECTION = ContactsContract.CommonDataKinds.Email.DATA + " <> '' AND "
+ Contacts.IN_VISIBLE_GROUP + "=1";
public static final String ORDER_BY = Contacts.SORT_KEY_PRIMARY;
@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
return new CursorLoader(this, ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, SELECTION, null,
ORDER_BY);
}
将结果传递Cursor
给适配器。获取联系人照片 URI,如下所示:
Uri photoUri = Contacts.getLookupUri(cursor.getLong(cursor.getColumnIndex(Contacts._ID)),
cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY)));
使用加载该照片Picasso
:
Picasso.with(context).load(photoUri).placeholder(R.drawable.ic_contact_picture).into(photo);
正如我所说,异常只发生在某些设备上。Picasso
不幸的是,将调用包装在 atry/catch
中没有任何作用,因为图像是异步加载的。我可以同步创建AsyncTask
并加载图像,但是使用的意义Picasso
何在?