2015-01-16 3 views
0

내가하려는 것은 SD 카드에 .vcf 파일을 생성하는 것입니다! 그러나 나는이 예외를 얻고있다. 누군가 나를 도울 수 있습니까?android에서 vcf 파일을 만들 때 오류가 발생했습니다.

The exception thrown

내가 사용 한 코드는 내가 그것을

때 java.io.IOException을주는 이유를 잘 모릅니다 이하 : 읽기에 실패했습니다 : EINVAL (잘못된 인수)를

public class Homepage extends ActionBarActivity { 
Cursor cursor; 
ArrayList<String> vCard ; 
String vfile; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_homepage); 

    @SuppressWarnings("unused") 
    final TextView re = (TextView) findViewById(R.id.email_hp); 

} 


public void createAndBackUp(View view) { 

    Thread createVCF = new Thread() { 
     public void run() { 
      try { 
       vfile = "backUpSiv.vcf"; 
       getVcardString(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    createVCF.start(); 

} 

public void getVcardString() { 
    // TODO Auto-generated method stub 
    vCard = new ArrayList<String>(); 
    cursor = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
      null, null); 
    if (cursor != null && cursor.getCount() > 0) { 
     cursor.moveToFirst(); 
     for (int i = 0; i < cursor.getCount(); i++) { 

      get(cursor); 
      Log.d("TAG", 
        "Contact " + (i + 1) + "VcF String is" + vCard.get(i)); 
      cursor.moveToNext(); 
     } 

    } else { 
     Log.d("TAG", "No Contacts in Your Phone"); 
    } 

} 

public void get(Cursor cursor) { 

    // cursor.moveToFirst(); 
    String lookupKey = cursor.getString(cursor 
      .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    Uri uri = Uri.withAppendedPath(
      ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
    AssetFileDescriptor fd; 
    try { 
     fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); 

     FileInputStream fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String vcardstring = new String(buf); 
     vCard.add(vcardstring); 

     String storage_path = Environment.getExternalStorageDirectory() 
       .toString() + File.separator + vfile; 
     FileOutputStream mFileOutputStream = new FileOutputStream(
       storage_path, true); 
     mFileOutputStream.write(vcardstring.toString().getBytes()); 

    } catch (Exception e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 
} 
+1

getVcardString()에서도 IndexOutOfBoundsException이 발생합니다. 어쩌면 이것이 문제일까요? (마지막 스크린 샷) – Sprigg

+0

도와 주실 수 있습니까? – Siv

+1

전체 충돌 스택 추적으로 질문을 업데이트 할 수 있습니까? 그렇지 않으면 문제를 추측해야합니다 (vCard.get()이 실패 할 것이라고 생각합니다). 그리고 예외가 있는지 찾아 볼 수 있습니다. vCard.get() – Sprigg

답변

1

Vcard를 만들려면이 방법을 사용하십시오.

public static String getVcard(String lookupKey, Context context, String filename) { 
    final String vfile = filename + ".vcf"; 

    Uri uri = Uri.withAppendedPath(
      ContactsContract.Contacts.CONTENT_VCARD_URI, 
      lookupKey); 
    AssetFileDescriptor fd; 
    FileOutputStream mFileOutputStream = null; 
    try { 
     fd = context.getContentResolver().openAssetFileDescriptor(uri, "r"); 
     FileInputStream fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String VCard = new String(buf); 
     String path = Environment.getExternalStorageDirectory() 
       .toString() + File.separator + "Demo-vcard" + File.separator; 

     File file = new File(path); 

     if (!file.exists()) { 
      file.mkdir(); 
     } 
     path = path + vfile; 

     mFileOutputStream = new FileOutputStream(path, 
       true); 
     mFileOutputStream.write(VCard.toString().getBytes()); 

     return path; 
    } catch (Exception e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } finally { 
     if (mFileOutputStream != null) { 
      try { 
       mFileOutputStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    return null; 
} 
관련 문제