0

I use the following to query the image of a contact:

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

As a result I get something like:

content://com.android.contacts/contacts/3699

Then I try to set it to an image view:

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);
                }

It raises an exception of file not found, however the System.out.println() in the catch block prints a bad bitmap uri. The above code was working properly some days ago, wonder whats going wrong.

The Log cat: (Check the first and the last line)

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
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • please follow https://github.com/loopj/android-smart-image-view – Jitesh Upadhyay Jan 14 '14 at 04:01
  • `Contacts.Photo.CONTENT_DIRECTORY` has been deprecated since API level 5, not seeing why you'r'e using it then. Check out `openContactPhotoInputStream` here: http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html and perhaps decode image before you set it to imageView with BitmapFactory.decodeStream of the inputstream you receive above – Magnus Jan 14 '14 at 22:00
  • The IDE does not give me a depreciation warning, so I was not aware. However I seem to have found the problem. I isolated the problem code and indeed the return values are different I am somehow getting a trimmed result in the problem project. Now I am trying to gauge why it is so. – Skynet Jan 15 '14 at 03:05

1 Answers1

0

The Uri that you have is not the exact file , Using that id you should retrieve the image using managedQuery

The idea will be like this

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

Community
  • 1
  • 1
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64