0

I'm storing my data to a file in /data/data/.....! And i want to add an import-export feature in my app that backups the file in the SDCARD and import it(and the app automatically read it). How can i doing this ? Thank you. So is that true ?

public class Import {
    public static void transfer(){  
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath() + "/SDCARD/Carburant");
    dir.mkdirs();
    copyfile("/data/data/carburant.android.com/files/","/SDCARD/Carburant/storeddata");

}

    private static void copyfile(String srFile, String dtFile){
        try{
          File f1 = new File("/data/data/carburant.android.com/files/");
          File f2 = new File("/SDCARD/Carburant/storeddata");
          InputStream in = new FileInputStream(f1);
          OutputStream out = new FileOutputStream(f2);

          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
          }
          in.close();
          out.close();
          System.out.println("File copied.");
        }
        catch(FileNotFoundException ex){
          System.out.println(ex.getMessage() + " in the specified directory.");
          System.exit(0);
        }
        catch(IOException e){
          System.out.println(e.getMessage());      
        }
      }

    }
androniennn
  • 3,117
  • 11
  • 50
  • 107

1 Answers1

2

first of all you need to assure you have the right permissions to read/write data.

Then, you can get the directory for your application like this:

String myDir = act.getFilesDir();

Regarding Android's security and sandboxing, your application will only be able to read/write to this directory, I think. Although SDCard access is more open:

You can call Environment.getExternalStorageDirectory() to get the root path to the SD card and use that to create a FileOutputStream.

A good example is given here in stackoverlow.

Community
  • 1
  • 1
chris polzer
  • 3,219
  • 3
  • 28
  • 44
  • So i have to initialize the transfer operation(all mounted, read/write persmissions), then copy the file in the /data/data..., then copying it to the dir in the SDCARD ! that's it ? – androniennn Apr 02 '11 at 13:14