在imageview中显示联系人图片。

时间:2014-01-14 03:57:37

标签: android bitmap android-imageview android-contacts filenotfoundexception

我使用以下内容查询联系人的图片:

Uri phUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,String.valueOf(contactId));

结果我得到了类似的东西:

内容://com.android.contacts/contacts/3699

然后我尝试将其设置为图像视图:

Uri IMAGE_URI = Uri.parse(image);
                IMAGE_URI= Uri.withAppendedPath(IMAGE_URI, Contacts.Photo.CONTENT_DIRECTORY);
                try{
                    ((ImageView) view.getTag(R.id.imageView1)).setImageURI(IMAGE_URI);

                }catch (Exception e){
                    System.out.println(e);
                }

它引发了一个未找到文件的异常,但是catch块中的System.out.println()会打印一个错误的位图uri。几天前上面的代码工作正常,不知道什么是错的。

Log cat :(检查第一行和最后一行)

01-14 09:25:19.271: W/ImageView(3674): Unable to open content: content://com.android.contacts/contacts/3699/photo
01-14 09:25:19.271: W/ImageView(3674): java.io.FileNotFoundException: content://com.android.contacts/contacts/3699/photo
01-14 09:25:19.271: W/ImageView(3674):  at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:149)
01-14 09:25:19.271: W/ImageView(3674):  at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:617)
01-14 09:25:19.271: W/ImageView(3674):  at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:739)
01-14 09:25:19.271: W/ImageView(3674):  at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:636)
01-14 09:25:19.271: W/ImageView(3674):  at android.content.ContentResolver.openInputStream(ContentResolver.java:471)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.ImageView.resolveUri(ImageView.java:631)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.ImageView.setImageURI(ImageView.java:379)
01-14 09:25:19.271: W/ImageView(3674):  at com.exa.birthdayrem.FragmentTab3$CurAdapter.bindView(FragmentTab3.java:157)
01-14 09:25:19.271: W/ImageView(3674):  at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.AbsListView.obtainView(AbsListView.java:2319)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.ListView.makeAndAddView(ListView.java:1793)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.ListView.fillDown(ListView.java:678)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.ListView.fillSpecific(ListView.java:1336)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.ListView.layoutChildren(ListView.java:1628)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.AbsListView.onLayout(AbsListView.java:2170)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.View.layout(View.java:13846)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewGroup.layout(ViewGroup.java:4466)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.View.layout(View.java:13846)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewGroup.layout(ViewGroup.java:4466)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.View.layout(View.java:13846)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewGroup.layout(ViewGroup.java:4466)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.View.layout(View.java:13846)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewGroup.layout(ViewGroup.java:4466)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.View.layout(View.java:13846)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewGroup.layout(ViewGroup.java:4466)
01-14 09:25:19.271: W/ImageView(3674):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.View.layout(View.java:13846)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewGroup.layout(ViewGroup.java:4466)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2168)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1926)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1140)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4726)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.Choreographer.doCallbacks(Choreographer.java:567)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.Choreographer.doFrame(Choreographer.java:536)
01-14 09:25:19.271: W/ImageView(3674):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
01-14 09:25:19.271: W/ImageView(3674):  at android.os.Handler.handleCallback(Handler.java:615)
01-14 09:25:19.271: W/ImageView(3674):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 09:25:19.271: W/ImageView(3674):  at android.os.Looper.loop(Looper.java:153)
01-14 09:25:19.271: W/ImageView(3674):  at android.app.ActivityThread.main(ActivityThread.java:5000)
01-14 09:25:19.271: W/ImageView(3674):  at java.lang.reflect.Method.invokeNative(Native Method)
01-14 09:25:19.271: W/ImageView(3674):  at java.lang.reflect.Method.invoke(Method.java:511)
01-14 09:25:19.271: W/ImageView(3674):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-14 09:25:19.271: W/ImageView(3674):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-14 09:25:19.271: W/ImageView(3674):  at dalvik.system.NativeStart.main(Native Method)
01-14 09:25:19.271: I/System.out(3674): resolveUri failed on bad bitmap uri: content://com.android.contacts/contacts/3699/photo

1 个答案:

答案 0 :(得分:0)

您拥有的Uri不是确切的文件。使用该ID,您应该使用managedQuery

检索图像

这个想法将是这样的

private void setContactInfo(long id){
Bitmap photoBitmap = null;
Uri contactUri = ContentUris.withAppendedId(
        ContactsContract.Contacts.CONTENT_URI, id);

Cursor cursor = managedQuery(contactUri, null, null, null, null);
cursor.moveToFirst();
contact_text.setText(cursor.getString(cursor
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));//contact.text is a     textView used to displays the contact name

String id = getIntent().getData().getLastPathSegment();
// Photo cursor

String photoWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
        + ContactsContract.Data.MIMETYPE + " = ?";
String[] photoWhereParams = new String[] { id,
        ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE };
Cursor photoCur = managedQuery(ContactsContract.Data.CONTENT_URI, null,
        photoWhere, photoWhereParams, null);
photoCur.moveToFirst();
if (photoCur.moveToFirst() && photoCur != null) {

    byte[] photoBlob = photoCur.getBlob(photoCur
            .getColumnIndex(Photo.PHOTO));
    if (photoBlob != null) {
        photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0,
                photoBlob.length);

        contact_image.setImageBitmap(photoBitmap);//contact_image is an ImageView
    } else {
        photoBitmap = BitmapFactory.decodeResource(getResources(),
                 android.R.drawable.ic_menu_report_image);//android.R.drawable.ic_menu_report_image is the default image if a Contact doesn't have any image stored
        contact_image.setImageBitmap(photoBitmap);
    }

}
cursor.close;
photoCur.close;         

}

source