0

I'm trying to understand how to work with files in Android

I have installed my android application in external storage

it contain a folder dataof files that it need while running...

firstly I think that my folder will saved automatically in the sdcard while installing

but after reading some question here , It seem I should create a folder in which I save my

folder data...

I write this question to ask about steps to do to achieve my goal an I need your advances

Goal

  • Save a folder data in external storage , read from it and write a new file

Steps

  • add permission to manifest file

  • create a folder in sdcard ==> how to place my folder there ?

  • create a new file , save it on sdcard and write into it

code

This is a part of my activity :

    {
      ....
       File file = new File ("...")

       doPrediction(file);


}

private void doPrediction (String FileName)

        {

     for (File child : VisualModels.listFiles()) {


     svmPredict.run(MyDataSource.inputPrediction,

         new File( MyDataSource.outputPrediction+ "/"+child.getName()+".predict"), 

         child);
          }
        }

I declared the class svmPredict as any ordinary java class ans this is the code of run method

       try 
    {
        BufferedReader input = new BufferedReader(new FileReader(inputFile));
        DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
        svm_model model = new svm().svm_load_model(modelFile);
        if(predict_probability == 1)
        {
            if(svm.svm_check_probability_model(model)==0)
            {
                System.err.print("Model does not support probabiliy estimates\n");
                System.exit(1);
            }
        }
        else
        {
            if(svm.svm_check_probability_model(model)!=0)
            {
                svm_predict.info("Model supports probability estimates, but disabled in prediction.\n");
            }
        }
        predict(input,output,model,predict_probability);
        input.close();
        output.close();
    } 
nawara
  • 1,157
  • 3
  • 24
  • 49
  • @blackbelt I tried to work with files as the traditional java way :put the foldes in the project folder and then read /write but It doesn't work situation in android in different – nawara Jun 15 '13 at 17:44
  • @blackbelt I can't find any exemple on google of how to using files in android , any tutorials are welcome – nawara Jun 15 '13 at 17:45
  • @blackbelt code is posted – nawara Jun 15 '13 at 17:53
  • what`s the value of MyDataSource.outputPrediction ? Do you have the WRITE_EXTERNAL_STORAGE permission? – Blackbelt Jun 15 '13 at 17:56
  • it is a File I have declared as a static final field in a final class `MyDataSource` – nawara Jun 15 '13 at 18:28

1 Answers1

0

Please read the Android documentation regarding External Storage.

Pay attention to the getExternalFilesDir(). You can retrieve the path to the external storage using this method. When you have the path, you can use the File object to create the data folder on the external storage like this:

File dataDirectory = new File(getExternalFilesDir() + "/data/");

Then create the folder by applying the mkdirs()-method on the File object:

dataDirectory.mkdirs();

To write to external storage, you will indeed have to declare this in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Stef Heylen
  • 294
  • 1
  • 9