簡體   English   中英

如何在SD卡中寫入數據

[英]how to write data in SD Card

我的應用程序是聯系人列表,我想將聯系人數據存儲在sd卡中,但是如何將數據存儲在sd卡中,或者如果我將使用數據庫,那么我想如何存儲它或使用簡單文件而不是我想如何存儲它

提前

如果你真的想將一個簡單的文件存儲到SD卡,你可以這樣做:

File sdCard = Environment.getExternalStorageDirectory(); // get a handle to the SD card
File myFile = new File(sdCard, "test"); // get a file handle so a single file

現在,您可以使用BufferedWriter ,或者使用任何其他Java方法寫入該新文件。

您的應用程序應該具有寫入SD卡的權限,當然,通過添加WRITE_EXTERNAL_STORAGE

按照慣例,所有應用程序數據應存儲在SD卡上的以下目錄中: Android/data/your.package.name/files/

另請注意,您應該明確檢查SD卡是否實際存在或是否可寫,等等。 請參閱此處獲取文檔。

您可以使用Environment.getExternalStorageDirectory()獲取路徑,然后使用該路徑打開文件。

您可能必須設置以下權限:

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

使用以下代碼將聯系人數據存儲到設備sdcard中。

VCardActivity.java:-

public class VCardActivity extends Activity {
      Cursor cursor;
      ArrayList<String> vCard;
      String vfile;
      static Context mContext;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mContext = VCardActivity.this;
            getVCF();
      }

      public static void getVCF() {
            final String vfile = "Contacts.vcf";
            Cursor phones = mContext.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                  null, null, null);
            phones.moveToFirst();
            for (int i = 0; i < phones.getCount(); i++) {
                   String lookupKey = phones.getString(phones
                           .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                   Uri uri = Uri.withAppendedPath(
                   ContactsContract.Contacts.CONTENT_VCARD_URI,
                                                 lookupKey);
                   AssetFileDescriptor fd;
                   try {
                           fd = mContext.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 + vfile;
                           FileOutputStream mFileOutputStream = new FileOutputStream(path,
                                     true);
                           mFileOutputStream.write(VCard.toString().getBytes());
                           phones.moveToNext();
                           Log.d("Vcard", VCard);
                   } catch (Exception e1) {
                           // TODO Auto-generated catch block
                           e1.printStackTrace();
                   }
            }
      }
}

並查看下面的鏈接以獲取更多信息。

將聯系人導出為VCF文件

並將以下權限授予您的androidmanifest.xml文件。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM