1

こんにちはiamは電話から私のarrayadapterに連絡先の画像を取得しようとしています

  @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()でそれを行う方法に驚いています。plzは連絡先から写真を取得する方法を提案します。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を拡張します{AdapterContactadapterContact; DisplayImageOptionsオプション; String [] imageUrls; ArrayList contactList = new ArrayList(); ListView listView; 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 に答える