问题
I am working on a small program on Android to write some hard-coded text to a file with a hard-coded name. While the operation is successful (there are no errors), I am not able to find the file on the emulator. My questions:
- Where are the files written?
- What is the file path I need for files to be stored on the home screen or the applications page (in general, a place where the UI can get to)?
The code:
public class MainActivity extends Activity {
Button writeFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writeFile = (Button) findViewById(R.id.writeFile);
writeFile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Step 1. Get file name.
String file = "hello_files.txt";
String str = "hello world";
Log.v(this.toString(), "Going to write to a file.");
FileOutputStream fos;
try {
fos = openFileOutput(file, MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
fos.write(str.getBytes());
Log.v(this.toString(), "Local path = ");
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v(this.toString(), "File not found!!");
e.printStackTrace();
} catch (IOException e) {
Log.v(this.toString(), "IO Exception.");
e.printStackTrace();
}
}
});
}
回答1:
Files are stored in "/mnt/sdcard/" (External storage) or "/data/data/Your Package Name/" (Internal storage) (as in this question). You can access this folder from this package only (unless the phone is rooted).
The second part of your question is not very clear. From what I can understand, Android is not like Windows (no desktop folder). To create a shortcut on the desktop, you need to implement an App widget.
来源:https://stackoverflow.com/questions/12998098/where-are-files-stored-on-an-android-emulator