마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Tread 를 사용했는데 너무 늦네요

0 추천
주소록을 아래와 같이 쓰레드를 사용해서 뿌렸는데 너무 늦어요

왜 늦는지 봐 주실래요?

----------------------------------------------------------------------

SimpleCursorAdapter mAdapter;
 MatrixCursor mMatrixCursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        // The contacts from the contacts content provider is stored in this cursor
        mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );
       
        // Adapter to set data in the listview
        mAdapter = new SimpleCursorAdapter(getBaseContext(),
                R.layout.lv_layout,
                null,
                new String[] { "name","photo","details"},
                new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);
       
        // Getting reference to listview
        ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
       
        // Setting the adapter to listview
        lstContacts.setAdapter(mAdapter);       
       
        // Creating an AsyncTask object to retrieve and load listview with contacts
        ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
       
        // Starting the AsyncTask process to retrieve and load listview with contacts
        listViewContactsLoader.execute();       
       
    }   
   
    /** An AsyncTask class to retrieve and load listview with contacts */
    private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor>{    

  @Override
  protected Cursor doInBackground(Void... params) {
   Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
   
   // Querying the table ContactsContract.Contacts to retrieve all the contacts
   Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
         ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
   
   if(contactsCursor.moveToFirst()){
    do{
     long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
     
     
     Uri dataUri = ContactsContract.Data.CONTENT_URI;
     
     // Querying the table ContactsContract.Data to retrieve individual items like
     // home phone, mobile phone, work email etc corresponding to each contact
     Cursor dataCursor = getContentResolver().query(dataUri, null,
           ContactsContract.Data.CONTACT_ID + "=" + contactId,
           null, null);
     
     
     String displayName="";
     String nickName="";
     String homePhone="";
     String mobilePhone="";
     String workPhone="";
     String photoPath="" + R.drawable.blank;
     byte[] photoByte=null;
     String homeEmail="";
     String workEmail="";
     String companyName="";
     String title="";
     
     
     
     if(dataCursor.moveToFirst()){
      // Getting Display Name
      displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
      do{
            
       // Getting NickName
       if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
        nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
       
       // Getting Phone numbers
       if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
        switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
         case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
          homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
          break;
         case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
          mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
          break;
         case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
          workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
          break;
        }
       }
       
       // Getting EMails
       if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {         
        switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
         case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
          homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
          break;
         case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
          workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
          break;          
        }
       }
       
       // Getting Organization details
       if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
        companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
        title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
       }
        
       // Getting Photo
       if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){        
        photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));
        
        if(photoByte != null) {       
         Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
         
         // Getting Caching directory
                        File cacheDirectory = getBaseContext().getCacheDir();
 
                        // Temporary file to store the contact image
                        File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");
 
                        // The FileOutputStream to the temporary file
                        try {
          FileOutputStream fOutStream = new FileOutputStream(tmpFile);
          
          // Writing the bitmap to the temporary file as png file
                         bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
 
                         // Flush the FileOutputStream
                         fOutStream.flush();
 
                         //Close the FileOutputStream
                         fOutStream.close();
 
         } catch (Exception e) {
          e.printStackTrace();
         }
 
                        photoPath = tmpFile.getPath();
        }
        
       }
       
      }while(dataCursor.moveToNext());     
      
      String details = "";
      
      // Concatenating various information to single string
      if(homePhone != null && !homePhone.equals("") )
       details = "HomePhone : " + homePhone + "\n";
      if(mobilePhone != null && !mobilePhone.equals("") )
       details += "MobilePhone : " + mobilePhone + "\n";
      if(workPhone != null && !workPhone.equals("") )
       details += "WorkPhone : " + workPhone + "\n";
      if(nickName != null && !nickName.equals("") )
       details += "NickName : " + nickName + "\n";
      if(homeEmail != null && !homeEmail.equals("") )
       details += "HomeEmail : " + homeEmail + "\n";
      if(workEmail != null && !workEmail.equals("") )
       details += "WorkEmail : " + workEmail + "\n";
      if(companyName != null && !companyName.equals("") )
       details += "CompanyName : " + companyName + "\n";
      if(title != null && !title.equals("") )
       details += "Title : " + title + "\n";
      
      // Adding id, display name, path to photo and other details to cursor
      mMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,photoPath,details});
     }
     
    }while(contactsCursor.moveToNext());
   }
   return mMatrixCursor;
  }
     
  @Override
  protected void onPostExecute(Cursor result) {   
   // Setting the cursor containing contacts to listview
   mAdapter.swapCursor(result);
  }  
    }
익명사용자 님이 2013년 7월 9일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...