1

I want to copy a file from /data/data... to the external SDCARD! However, i'm finding this problem: Log message: 04-04 12:01:19.271: DEBUG/Carburant(9623): /username.usercar.settings.dat (No such file or directory)

I guess that i can't simply access to this file without an "extra" code. Here is my code(the necessary lines):

File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath() + "/Carburant/");
    dir.mkdirs();
    copyfile(nom,sdCard.getAbsolutePath() + "/Carburant/storeddata.dat");

public Import(Context context,String nom) {
        this.context = context;
        this.nom=nom;
       }

The lines where the function is called:

case R.id.exporter:
            final SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(context);
    String fileName = getResources().getString(R.string.fileName);
    fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
    Import myImport = new Import(this,fileDir+fileName);
            myImport.transfer();
            return true;

Android Manifest(necessary code):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="carburant.android.com"
      android:versionCode="1" android:versionName="0.1">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-sdk android:minSdkVersion="8" />

Copy file func:

private void copyfile(String srFile, String dtFile){
        try{
            File f1 = new File(srFile);
            File f2 = new File(dtFile);
          InputStream in = new FileInputStream(f1);
          OutputStream out = new FileOutputStream(f2);

          byte[] buf = new byte[4096];
          int len;
          while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
          }
          in.close();
          out.close();
          Toast.makeText(context, "Export effectué", Toast.LENGTH_SHORT).show();
        }
        catch(FileNotFoundException ex){
            Toast.makeText(context, "File Not found", Toast.LENGTH_SHORT).show();
            String x=ex.getMessage();
            Log.d("Carburant", x);
        }
        catch(IOException e){
            Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();      
        }
      }

So, what misses? Thanks.

androniennn
  • 3,117
  • 11
  • 50
  • 107

1 Answers1

3

To get you file located in the "files" dir (assuming its name is settings.dat), use the following method:

String filePath = this.getFilesDir().getAbsolutePath() + File.separator + "settings.dat";
Import myImport = new Import(this,filePath);

If the file itself is a raw resource, follow the answer to this question to copy it.

(make sure you have permission to write to external storage...)

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
  • @MByD:I have a problem with "yourActivity"! my current activity that have the call of Import object is "Saves". When i put String mainDirPath = Save.getFilesDir().getParent(); i have error. – androniennn Apr 05 '11 at 01:18
  • @MByD: This this the error message: Cannot make a static reference to the non-static method getFilesDir() from the type ContextWrapper – androniennn Apr 05 '11 at 01:18
  • 1
    you should call the method `this.getFilesDir().getAbsolutePath(); ` not `Save.getFilesDir().getAbsolutePath(); ` since this is a method that shoul be called on object, not class. – MByD Apr 05 '11 at 01:21
  • @MByd: sorry, i didn't notice that! anyway, i compiled and the error is gone! however, i have a second error: 04-05 01:22:59.291: DEBUG/Carburant(5850): /mnt/sdcard/Carburant/storeddata.dat (Permission denied) – androniennn Apr 05 '11 at 01:24
  • @MByD: i have permission to write to external storage(added to manifest)! What could it be ? – androniennn Apr 05 '11 at 01:25
  • Maybe the permissions are not positioned correctly in the manifest. – MByD Apr 05 '11 at 01:26
  • 1
    @androniennn - check this post: http://stackoverflow.com/questions/4506612/android-sd-card-writing-permission-denied – MByD Apr 05 '11 at 01:35
  • @MByD: thank you for he link. I adjusted the code to my code, to see what is exactly yhe problem! and i have this in the log message: 04-05 01:42:41.191: DEBUG/Carburant(8457): Sdcard was not mounted !! Why it's not mounted? emulator problem ? – androniennn Apr 05 '11 at 01:44
  • I haven't noticed you are using an emulator. see this link http://www.androiddevelopment.org/2008/11/11/how-to-create-and-use-the-sd-card-with-the-android-emulator/ – MByD Apr 05 '11 at 01:46
  • and if you already created it, check this out: http://stackoverflow.com/questions/5174824/android-emulator-sdcard-suddenly-read-onlyfailing-to-mount – MByD Apr 05 '11 at 01:47
  • @MByD: i maked this, and always the same problem. Plus, i copied the apk to my phone to see if it works, and i have a file not found exception. – androniennn Apr 05 '11 at 01:50
  • @androniennn - please open a new question for this, with a full trace etc. since it becomes pretty messy in comments :) – MByD Apr 05 '11 at 01:53
  • @MByD: Okay, i'll open a new topic, tomrrow, now time to sleep ;).Thank you for your help! and i think that the main problem of this topic is solved(how to copy a file from /data/data...). I hope that you'll help me when i open a new question :). – androniennn Apr 05 '11 at 01:56
  • @MByD: one more question please: i debugged now to see what it gives as output ("nom" variable). I have /data/data/carburant.android.com. (not with files/settings.dat). Is that normal ? – androniennn Apr 05 '11 at 01:58
  • 1
    no. you should pass something like this to import: `this.getFilesDir() + File.separator + "settings.dat"` – MByD Apr 05 '11 at 02:01
  • @MByD: like this ? String mainDirPath = this.getFilesDir()+ "settings.dat".getParent(); => error message. – androniennn Apr 05 '11 at 02:04
  • @MByD: Oh yes, now i have "mainDirPath" returning all the path file ! But always the problem of the SDCARD not mounted. Really thank you very very much for your help. Cheers. – androniennn Apr 05 '11 at 02:14