سؤال

I am trying insert and retrieve some data into a specific path inside External and Internal Storage.I have found an example on the net.ıts succesfully done.

this is my code to save and retrieve

public class MainActivity extends Activity implements OnClickListener{

 private String filename = "MySampleFile.txt";
 private String filepath = "MyFileStorage";
 File myInternalFile;
 File myExternalFile;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
  File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
  myInternalFile = new File(directory , filename);

  Button saveToInternalStorage =
   (Button) findViewById(R.id.saveInternalStorage);
  saveToInternalStorage.setOnClickListener(this);

  Button readFromInternalStorage =
   (Button) findViewById(R.id.getInternalStorage);
  readFromInternalStorage.setOnClickListener(this);

  Button saveToExternalStorage =
   (Button) findViewById(R.id.saveExternalStorage);
  saveToExternalStorage.setOnClickListener(this);
 -
  Button readFromExternalStorage =
   (Button) findViewById(R.id.getExternalStorage);
  readFromExternalStorage.setOnClickListener(this);

  //check if external storage is available and not read only 
  if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
   saveToExternalStorage.setEnabled(false);
  }
  else {
   myExternalFile = new File(getExternalFilesDir(filepath), filename);
  }

 }

 public void onClick(View v) {

  EditText myInputText = (EditText) findViewById(R.id.myInputText);
  TextView responseText = (TextView) findViewById(R.id.responseText);
  String myData = "";

  switch (v.getId()) {
  case R.id.saveInternalStorage:
   try {
    FileOutputStream fos = new FileOutputStream(myInternalFile);
    fos.write(myInputText.getText().toString().getBytes());
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   myInputText.setText("");
   responseText
   .setText("MySampleFile.txt saved to Internal Storage...");
   break;

  case R.id.getInternalStorage:
   try {
    FileInputStream fis = new FileInputStream(myInternalFile);
   // DataInputStream in = new DataInputStream(fis);
    BufferedReader br =
     new BufferedReader(new InputStreamReader(fis));
    String strLine;
    while ((strLine = br.readLine()) != null) {
     myData = myData + strLine;
    }
    br.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   myInputText.setText(myData);
   responseText
   .setText("MySampleFile.txt data retrieved from Internal Storage...");
   break;

  case R.id.saveExternalStorage:
   try {
    FileOutputStream fos = new FileOutputStream(myExternalFile);
    fos.write(myInputText.getText().toString().getBytes());
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   myInputText.setText("");
   responseText
   .setText("MySampleFile.txt saved to External Storage...");
   break;

  case R.id.getExternalStorage:
   try {
    FileInputStream fis = new FileInputStream(myExternalFile);
    DataInputStream in = new DataInputStream(fis);
    BufferedReader br =
     new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null) {
     myData = myData + strLine;
    }
    in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   myInputText.setText(myData);
   responseText
   .setText("MySampleFile.txt data retrieved from Internal Storage...");
   break;


  }
 }

Then I wanted to retreieve all the sdcard paths to understand if my path created or not ..such as

storage/sdcard/MyFileStorage.. I am using this code for this

public class MainActivity extends ListActivity {

List<String>fileList=new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File root=
                new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        listDir(root);
      ArrayAdapter<String>adapter=
              new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,fileList);
   this.setListAdapter(adapter);

    }

    private void listDir(File f) {

        File[]files=f.listFiles();
        for (File file : files) {
            fileList.add(file.getPath());

        }
    }



}

But I excepted to see this path below.But Its not exist.What am i doing wrong

storage/sdcard/MyFileStorage 
هل كانت مفيدة؟

المحلول

myExternalFile = new File(getExternalFilesDir(filepath), filename);

This line actually creates file in /storage/sdcard0/Android/data/your.application.name/files/MyFileStorage

Read documents about getExternalFilesDir

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top