我无法在我的假Sdcard中列出文件

时间:2014-05-09 13:20:56

标签: android

我正在尝试插入并检索一些数据到ExternalStorage中的特定路径。然后我尝试检索所有的SD卡路径,以了解我的路径是否创建。但我无法在列表中看到我的路径。我已经意识到我的路径是在不同的位置创建的(它必须在我创建的mysd.img中)这段代码应该是什么样才能看到我的路径。

这低于我的路径字符串:

private String filepath = "MyFileStorage";

我正在使用此代码:

此代码

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

        }
    }



}

这是我保存和检索的代码

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;


  }
 }

0 个答案:

没有答案