我正在尝试将一些数据插入并检索到外部和内部存储中的特定路径中。我在 net.ı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;
}
}
然后我想检索所有 sdcard 路径以了解我的路径是否创建..例如
storage/sdcard/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());
}
}
}
但我除了看到下面这条路径。但它不存在。我做错了什么
storage/sdcard/MyFileStorage