如何将.vcf文件存储在内部SDCard的单独文件夹中

时间:2014-11-01 05:46:18

标签: android android-sdcard vcf

我正在使用以下代码来存储我从代码生成的vcf文件 我能够生成vcf个文件 但它出现在SDcard的根目录下 我想将它存储在单独的文件夹下 我如何满足要求? Plz的帮助 谢谢你。

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");
        }

}

1 个答案:

答案 0 :(得分: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)以确保外部存储实际上可用。