1

我正在尝试在文件夹中的SD 卡上备份我的收件箱短信。但我无法在SD 卡上创建文件夹我正在使用此代码

backup=(Button)findViewById(R.id.backup);
backup.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        backupSMS();
    }
         public ArrayList<String> smsBuffer = new ArrayList<String>();
           String smsFile = "SMS-" + SystemClock.currentThreadTimeMillis() + ".csv";

    private void backupSMS() {
        smsBuffer.clear();
        Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
        Cursor cursor1 = getContentResolver().query(
                mSmsinboxQueryUri,
                new String[] { "_id", "thread_id", "address", "person", "date",
                        "body", "type" }, null, null, null);
        //startManagingCursor(cursor1);
        String[] columns = new String[] { "_id", "thread_id", "address", "person", "date", "body",
                "type" };
        if (cursor1.getCount() > 0) {
            String count = Integer.toString(cursor1.getCount());
            Log.d("Count",count);
            while (cursor1.moveToNext()) {

                 String messageId = cursor1.getString(cursor1
                        .getColumnIndex(columns[0]));

                 String threadId = cursor1.getString(cursor1
                        .getColumnIndex(columns[1]));

                String address = cursor1.getString(cursor1
                        .getColumnIndex(columns[2]));
                String name = cursor1.getString(cursor1
                        .getColumnIndex(columns[3]));
                String date = cursor1.getString(cursor1
                        .getColumnIndex(columns[4]));
                String msg = cursor1.getString(cursor1
                        .getColumnIndex(columns[5]));
                String type = cursor1.getString(cursor1
                        .getColumnIndex(columns[6]));



                smsBuffer.add(messageId + ","+ threadId+ ","+ address + "," + name + "," + date + " ," + msg + " ,"
                        + type);

            }           
            generateCSVFileForSMS(smsBuffer);
        }               
    }


     private void generateCSVFileForSMS(ArrayList<String> list)
    {

        try 
        {
            String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + smsFile;
            FileWriter write = new FileWriter(storage_path);

            write.append("messageId, threadId, Address, Name, Date, msg, type");
            write.append('\n');
            write.append('\n');


            for (String s : list)
            {
                write.append(s);
                write.append('\n');
            }
            write.flush();
            write.close();
        }

        catch (NullPointerException e) 
        {
            System.out.println("Nullpointer Exception "+e);
             //  e.printStackTrace();
         }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
       }

    }
});  

使用此代码,我可以备份但不能在文件夹中。请帮助我,我是 android 新手,在此先感谢

4

2 回答 2

9

还要检查是否安装了 sdcard

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
   // sd card mounted
}   

File direct = new File(Environment.getExternalStorageDirectory() + "/YourFolder");

if(!direct.exists())
{
    if(direct.mkdir()) 
      {
       //directory is created;
      }

}

我忘了提到你需要在清单中提供权限

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

有了上述权限,您也将默认拥有阅读权限。

另外最好使用File.seperator而不是/

于 2013-02-28T15:04:13.270 回答
6

如果您创建一个包装顶级目录的 File 对象,您可以调用它的 mkdirs() 方法来构建所有需要的目录。就像是:

// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);

注意:使用 Environment.getExternalStorageDirectory() 获取“SD 卡”目录可能是明智的,因为如果手机附带的东西不是 SD 卡(例如内置闪存,a'la the苹果手机)。无论哪种方式,您都应该记住,您需要检查以确保它确实存在,因为 SD 卡可能已被移除。

更新:由于 API 级别 4 (1.6),您还必须请求许可。像这样的东西(在清单中)应该可以工作:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-02-28T15:02:18.440 回答