Java 对外部存储感到困惑吗

Java 对外部存储感到困惑吗,java,android,android-external-storage,Java,Android,Android External Storage,我正在尝试将一些数据插入并检索到外部和内部的特定路径中 内部存储。我在网上找到了一个例子。ıts做得很成功 这是我要保存和检索的代码 public class MainActivity extends Activity implements OnClickListener{ private String filename = "MySampleFile.txt"; private String filepath = "MyFileStorage"; File myInternalFile

我正在尝试将一些数据插入并检索到外部和内部的特定路径中 内部存储。我在网上找到了一个例子。ıts做得很成功

这是我要保存和检索的代码

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;


  }
 }
然后我想检索所有SD卡路径,以了解我的路径是否已创建..例如

存储/SD卡/MyFiles存储。。 我正在为此使用此代码

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

        }
    }



}
myExternalFile=新文件(getExternalFilesDir(filepath),文件名)

这一行实际上在
/storage/sdcard0/Android/data/your.application.name/files/MyFileStorage


使用我获得的代码阅读有关
getExternalFilesDir

的文档
05-09 14:27:05.580:W/ApplicationContext(21200):无法创建外部文件目录
谢谢您的回复,但我很困惑。yo说该行在不同的位置创建了我的文件。那么我如何才能使用此代码设置根位置/storage/sdcard0/Android/data此文件根=新文件(Environment.getExternalStorageDirectory().getAbsolutePath())<代码>环境。getExternalStorageDirectory()。getAbsolutePath()将返回
/storage/sdcard0/
storage/sdcard/MyFileStorage