Ashfaque
Ashfaque

Reputation: 1284

How to make folder in sdcard in android

i am trying to backup my inbox sms on SD card in a folder. but i am not able to make folder on SD card i am using this code

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

    }
});  

with this code i am able to backup but not in a folder. please help me i am new to android thanks in advance

Upvotes: 1

Views: 9552

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Also check if sdcard is mounted

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

}

I forgot to mention that you need to provide permission in manifest

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

With the above permission you will have read permission also by default.

Also it is better to use File.seperator instead of /

Upvotes: 9

X-Factor
X-Factor

Reputation: 2117

If you create a File object that wraps the top-level directory you can call it's mkdirs() method to build all the needed directories. Something like:

// 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);

Note: It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change if a phone comes along which has something other than an SD Card (such as built-in flash, a'la the iPhone). Either way you should keep in mind that you need to check to make sure it's actually there as the SD Card may be removed.

UPDATE: Since API Level 4 (1.6) you'll also have to request the permission. Something like this (in the manifest) should work:

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

Upvotes: 6

Related Questions