2014-02-26 3 views
0

Я использую CursorAdapter ниже, чтобы отображать контакты, и он работает. Но я не могу получить фотографии для отображения.Отображение фотографий с ContactContract Android

Сначала я только что отправил PHOTO_THUMBNAIL_URI и использовал pic.setImageURI. Но это работало только на 2-3 фотографии, и эти фотографии иногда использовались иногда. Было какое-то странное поведение, если не сказать больше.

Оглядываясь в Интернете, я не могу найти решение, которое работает. Ниже приведенное ниже решение не относится к CRASH моему приложению, но оно не отображает никаких изображений.

Я пробовал с поставкой как PHOTO_THUMBNAIL_URI, PHOTO_URI, _ID и PHOTO_FILE_ID без успеха.

Что я делаю неправильно? :(

public static class CustomAdapter extends SimpleCursorAdapter { 

    private int mSelectedPosition; 
    private Context context; 
    private int layout; 

    @Override 
    public View newView(final Context context, Cursor cursor, ViewGroup parent) { 

     final Cursor c = getCursor(); 

     final LayoutInflater inflater = LayoutInflater.from(context); 
     View v = inflater.inflate(layout, parent, false); 

     TextView name_text = (TextView) v.findViewById(R.id.KontakterNamn); 
     ImageView pic = (ImageView) v.findViewById(R.id.RealKontakter_bild); 

     Bitmap photo = null; 

     Uri displayPhotoUri = ContentUris.withAppendedId(ContactsContract.DisplayPhoto.CONTENT_URI, c.getColumnIndex("PHOTO_THUMBNAIL_URI")); 
     try { 
      AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(
        displayPhotoUri, "r"); 
      photo = BitmapFactory.decodeStream(fd.createInputStream()); 
      pic.setImageBitmap(photo); 
     } catch (IOException e) { 
      // do nada 
     } 

     int nameCol = c.getColumnIndex("DISPLAY_NAME"); 
     final String name = c.getString(nameCol); 

     name_text.setText(name); 

     return v; 
    } 


    public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     this.context = context; 
     this.layout = layout; 
    } 


    @Override 
    public void bindView(View v, final Context context, Cursor c) { 

     TextView name_text = (TextView) v.findViewById(R.id.KontakterNamn); 
     ImageView pic = (ImageView) v.findViewById(R.id.RealKontakter_bild); 

     Bitmap photo = null; 

     Uri displayPhotoUri = ContentUris.withAppendedId(ContactsContract.DisplayPhoto.CONTENT_URI, c.getColumnIndex("PHOTO_THUMBNAIL_URI")); 
     try { 
      AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(
        displayPhotoUri, "r"); 
      photo = BitmapFactory.decodeStream(fd.createInputStream()); 
      pic.setImageBitmap(photo); 
     } catch (IOException e) { 
      // 
     } 

     int nameCol = c.getColumnIndex("DISPLAY_NAME"); 
     final String name = c.getString(nameCol); 

     name_text.setText(name); 

     int position = c.getPosition(); 

    } 


    public void setSelectedPosition(int position) { 
     mSelectedPosition = position; 
     notifyDataSetChanged(); 

    } 
} 
static CustomAdapter mCursorAdapter; 
+0

Посмотрите на это http://stackoverflow.com/questions/ 15860994/получить контакт-фото-ориентированных контактного-PhoneNumber-андроид-рабочего пример –

ответ

0

Попробуйте как следующий

byte[] imageInByte; 
String imageuri = null; 

      imageuri = cursor.getString(cursor 
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)); 

      if (imageuri == null) { 
       Bitmap image = BitmapFactory.decodeResource(getResources(), 
         R.drawable.ic_launcher); 

       // convert bitmap to byte 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       image.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
       imageInByte = stream.toByteArray(); 

      } else { 

       try { 
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(
          this.getContentResolver(), Uri.parse(imageuri)); 

        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
        imageInByte = stream.toByteArray(); 

       } catch (Exception e) { 
        e.getMessage(); 

       } 

      } 

и после этого установить, что байт с вашей точки зрения изображения

byte[] mbitmap = imageInByte; 
      Bitmap bitmap = BitmapFactory.decodeByteArray(mbitmap, 0, mbitmap.length); 

      ((ImageView)view).setImageBitmap(bitmap); 
Смежные вопросы