我找不到我的应用程序在Android中创建的文件的位置
你好,我的代码有两个按钮保存并读取当我按下保存它将数据保存到SampleFile.xml,当我按下加载XML的内容文件显示在设备的屏幕上。我的应用程序工作,但是当我试图找到SampleFile.xml时,无处可寻。我想我的应用程序仿真程序和物理设备我找不到我的应用程序在Android中创建的文件的位置
public class MainActivity extends Activity {
EditText inputText;
TextView response;
Button saveButton,readButton;
private String filename = "SampleFile.xml";
private String filepath = "MyFileStorage";
File myExternalFile;
String myData = "<question> "+ Test.s +" <qusetion>\n";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (EditText) findViewById(R.id.myInputText);
response = (TextView) findViewById(R.id.response);
saveButton =
(Button) findViewById(R.id.saveExternalStorage);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(myData.toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
inputText.setText("");
response.setText("SampleFile.xml saved to External Storage..");
}
});
readButton = (Button) findViewById(R.id.getExternalStorage);
readButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
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();
}
inputText.setText(myData);
response.setText("SampleFile.xml data retrieved from External Storage...");
}
});
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveButton.setEnabled(false);
}
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
private static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}
试图找到Android的文件/数据/数据/ your_package/MyFileStorage/SampleFile.xml
此路径不应硬编码。相反,OP应该使用“Resources”对象来查询内部存储目录。 –
@ Code-Apprentice我不告诉他在这条路上保存文件。但他的代码保存在这条路径上的文件。他可以在这里找到他的代码 – GiaLe
应用程序的目录出现在这个路径中,但它的空 – AndroidKid
你永远初始化'myExternalFile'变量。 – lionscribe
尝试打印并记录您用来存储文件的路径。像这样:Log.d(“PATH”,新文件(getExternalFilesDir(文件路径),文件名)); –
@lionscribe是他在这一行:myExternalFile = new File(getExternalFilesDir(filepath),filename); –