1

嗨,我正在尝试将手机中的联系人图像检索到我的阵列适配器中

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) (getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE));
            view = inflater.inflate(renderer, null);
        }


        TextView text = (TextView) view.findViewById(R.id.name);
        ImageView photo = (ImageView) view.findViewById(R.id.photo);
        new LoadImage(photo).execute();

我想使用 asynctask 从手机中获取照片,我很惊讶如何在 doinbackground() 中做到这一点。请建议如何从联系人中获取照片,doinbackground() 过程中的内容

class LoadImage extends AsyncTask<Object, Void, Bitmap>{

        private ImageView imv;


        public LoadImage(ImageView imv) {
             this.imv = imv;

        }



    @Override
    protected Bitmap doInBackground(Object... params) {
        Bitmap bitmap = null;
        photo.setImageBitmap(contact.getPhoto());
    }
    @Override
    protected void onPostExecute(Bitmap result) {

        }
    }
4

1 回答 1

1

你可以使用universalimageloader来做到这一点https://github.com/nostra13/Android-Universal-Image-Loader

正如您在下面的示例中看到的那样,您可以将联系人照片保存到 SD 购物车,然后将他们的 FilePath 放入列表中,并将该路径与通用图像加载器一起使用,

您可以从https://dl.dropbox.com/u/68130108/UniversalImageLoaderExample.rar获取完整示例

在你的适配器 getView

imageLoader.displayImage(contactFilePath, holder.image, displayImageOptions);

在您的活动公共类 ImageListActivity 扩展 BaseActivity { AdapterContact adapterContact; DisplayImageOptions 选项;字符串[] imageUrls; ArrayList 联系人列表 = 新的 ArrayList(); 列表视图列表视图;ProgressDialog 对话框;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_list);

        listView = (ListView) findViewById(android.R.id.list);

        new asynGetContacts().execute();

    }

    protected class asynGetContacts extends AsyncTask<String, Void, Integer> {      
        protected Integer doInBackground(String... params) {
            try {
                ImageListActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        dialog = ProgressDialog.show(ImageListActivity.this, "","Lütfen bekleyin...", true);
                        dialog.show();
                    }
                });
                Uri uri = ContactsContract.Contacts.CONTENT_URI;
                ContentResolver cr = getContentResolver();
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC";
                Cursor cur = cr.query(uri, null, null, null, sortOrder);
                if (cur.getCount() > 0) {
                    String id;
                    String name;
                    while (cur.moveToNext()) {
                        Contact c = new Contact();
                        id = cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts._ID));
                        name = cur
                                .getString(cur
                                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Uri my_contact_Uri = Uri.withAppendedPath(
                                ContactsContract.Contacts.CONTENT_URI,
                                String.valueOf(id));

                        InputStream inputStream = ContactsContract.Contacts
                                .openContactPhotoInputStream(getContentResolver(),
                                        my_contact_Uri);
                        BufferedInputStream buf = new BufferedInputStream(inputStream);
                        Bitmap my_btmp = BitmapFactory.decodeStream(buf);

                        if (my_btmp != null) {
                            c.photo = my_btmp;
                            c.id = id;
                            c.name = name;
                            c.photoURL = saveToSD(my_btmp, id);
                            contactList.add(c);
                        }
                    }
                }
                cur.close();



            } catch (Exception e) {
                Log.v("hata", e.toString());
                return 0;
            }

            return 1;
        }

        protected void onPostExecute(Integer result) {
            try {
                dialog.dismiss();
                adapterContact = new AdapterContact(ImageListActivity.this, 0,
                        contactList);
                listView.setAdapter(adapterContact);
                listView.setOnScrollListener(new PauseOnScrollListener(false, true));
            } catch (Exception e) {
                Log.v("hata", e.toString());
            }

            super.onPostExecute(result);
        }
    }

    public String saveToSD(Bitmap bmp, String id) { 
        String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ContactImage/";

        try {
            File dir = new File(file_path);

            if (!dir.exists())
                dir.mkdirs();

            File f = getFileStreamPath(file_path);

            if (!f.exists()) {
                File file = new File(dir, "contact_" + id + ".png");
                FileOutputStream fOut = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
                fOut.flush();
                fOut.close();
            }


        } catch (Exception e) {
            Log.v("Hata", e.toString());
        }

        return "file://"+file_path+"contact_" + id + ".png";
    }

}
于 2012-12-18T19:14:50.513 回答