如何向SD卡写入数据

发布于 2024-09-30 07:10:31 字数 106 浏览 3 评论 0原文

我的应用程序是联系人列表,我想将我的联系人数据存储在 SD 卡中 但是我如何将数据存储在SD卡中,或者如果我将使用数据库,那么我想如何存储它,或者使用简单的文件而不是我想

提前存储它

my application is contact list and i want to store my contact data in sd card
but how can i store data in sd card, or if i will use database then how i want to store it or using simple file than how i want to store it

thanx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一萌ing 2024-10-07 07:10:31

如果您确实想将一个简单的文件存储到 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 卡是否确实存在存在或者是否可写,等等。请参阅此处获取文档。

If you really want to store a simple file to the SD card, you can do it like so:

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

Now you can use a BufferedWriter, for example, or any other Java method of writing to that new file.

Your app should have the permission to write to the SD card, of course, by adding WRITE_EXTERNAL_STORAGE.

By convention, all your application data should be stored in the following directory on the SD card: Android/data/your.package.name/files/

Also note that you should explicitly check whether a SD card actually exists or if its writable, and so on. See here for a documentation.

强者自强 2024-10-07 07:10:31

您可以使用Environment.getExternalStorageDirectory() 获取路径,然后使用该路径打开文件。

您可能必须具有以下权限集:

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

You can get the path using Environment.getExternalStorageDirectory() and then open a file using that path.

You may have to have the following permission set:

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
雨巷深深 2024-10-07 07:10:31

使用以下代码将联系人数据存储到设备 SD 卡中。

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" />

Store contact data into device sdcard using below code.

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

and see below link for more information.

Export Contacts as a VCF file

and give below permission into your androidmanifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文