2014-11-01 73 views
0

我使用下面的代碼來存儲vcf文件我從代碼 產生我能夠生成vcf文件 但它的根目錄下出現在SD卡 我想將其存儲在獨立文件夾下 我如何滿足要求? Plz幫助 謝謝。如何在單獨的文件夾.vcf文件存儲在SD卡內

private void getVcardString() throws IOException { 

     final String vfile = "BackupCont" + currDate + ".vcf"; 
     vCard = new ArrayList<String>(); 

     cursor = getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
       null, null); 
     Cursor c = dbhandle.getContactsByIsChecked(); 
     ArrayList<String> arrchk=new ArrayList<String>(); 
     for(int cc=0;cc<c.getCount();cc++) 
     { 
      arrchk.add(c.getString(c.getColumnIndex("con_name"))); 
     } 

     if (cursor != null && cursor.getCount() > 0) { 

      int i; 

      String new_path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/Folder_name"; 
      // String path = "c:/"; 
      File dir = new File(new_path); 

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

      String storage_path = Environment.getExternalStorageDirectory()+"/Folder_name"+vfile; 
      Toast.makeText(getApplicationContext(), storage_path,Toast.LENGTH_LONG).show(); 
      FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false); 

      cursor.moveToFirst(); 

      for (i = 0; i < cursor.getCount(); i++) 
      { 

       if(arrchk.contains(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)))) 
       { 
        get(cursor); 
        cursor.moveToNext(); 
        mFileOutputStream.write(vCard.get(i).toString().getBytes()); 
       } 

      } 

      mFileOutputStream.close(); 
      cursor.close(); 

      Toast.makeText(getApplicationContext(), "Created...",Toast.LENGTH_LONG).show(); 

      Uri uri=Uri.fromFile(new File(storage_path,""+vfile)); 
      Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
      sharingIntent.setType("application/x-vcard"); 

      sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri); 
      startActivity(Intent.createChooser(sharingIntent, "Share using")); 

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

} 

回答

0
String outputDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "subFolderName"; 

outputDirectory.mkdirs(); 

File outputFile = new File(outputDirectory, filename); 

FileOutputStream fos = new FileOutputStream(outputFile); 

您也應該檢查Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED),以確保外部存儲實際上是可用。

+0

謝謝你zaventh 你的答案是有用的 現在它的工作正常 – Suraj 2014-11-01 10:00:51