如何在android中的sdcard中创建文件夹

时间:2013-02-28 14:59:31

标签: android sd-card android-sdcard

我正在尝试备份我的收件箱短信在文件夹中的 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新手,提前感谢

2 个答案:

答案 0 :(得分: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代替/

答案 1 :(得分: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()来获取&#34; SD卡&#34;目录,因为这可能会改变,如果手机出现其他东西而不是SD卡(如内置闪存,iPhone等)。无论哪种方式,您都应该记住,您需要检查以确保它实际存在,因为可能会移除SD卡。

更新:由于API级别4(1.6),您还必须请求权限。这样的事情(在清单中)应该有效:

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