Internal storage files location - android

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Ok so the above code creates a file in the internal storage and writes something into it. My question is, where can I find and open this file in Eclipse? I want to see its contents and edit it manually. Or I may want to include my own file and read its contents.

My question is, where can I find and open this file in Eclipse?
Using the file manager in the DDMS perspective, for the default user account on an emulator, go to /data/data/your.application.id.goes.here/files, substituting in the package from your manifest where I have your.application.id.goes.here.

Related

Android Studio Data Collection

I am designing an app for my job to collect data on dog training. I need to be able to access the data on my computer to analyze it. I have completed the front end of the app and organized all of the information into a single string that I would like to save into a file for later analysis. I run the app and can not find the data anywhere on the tablet that runs the app. The code that saves the app is:
String output="Example, Data";
File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS),"TrainingData.txt");
try {
FileOutputStream fout = new FileOutputStream(file);
OutputStreamWriter outputStream = new OutputStreamWriter(fout);
outputStream.write(output);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Is there something wrong with the saving part of the code listed or is it correct and I'm just failing to find the file on the tablet. Any help would be greatly appreciated.
Try this instead:
File path = context.getFilesDir();
File file = new File(path, "the-file.txt");
and then:
FileOutputStream stream = new FileOutputStream(file);
try {
stream.write(output".getBytes());
} finally {
stream.close();
}

Android data file location in phone

Where is the android data folder exists ?since i have to save large files and users should not able to see them ,i think to use data folder.But i don't know where is located in android phone.does it is on sdcard or not ?
You should use Internal storage
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.
Sample code:
File file = new File(context.getFilesDir(), filename);
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
catch (IOException e) {
// Error while creating file
}
return file;
}
For more details, please refer here.
Use the below to get the path of of data directory
File f = Environment.getDataDirectory();
System.out.println(f.getAbsolutePath());

What directory will FileOutputStream save my simple file in(Android)? How do I find out if it has saved?

I'm trying to recreate a simple text editing Android app and so far I have this code to save the file:
String filename = "myfile";
String content = edit.getText().toString();
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename,
Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
However I have no idea where in the internal storage this is being saved. How can I access/look at this directory to confirm the file has actually saved. I cant seem to find the file in File explorer in Eclipse (Heck, I can't even find the app name).Also, how would I go about loading this file once it is located? Thanks.
openFileOutput(filename,
Context.MODE_PRIVATE); is application private directory output stream api. Your file will saved in your /data/data/your_packagename directory.

Error writing to SD Card

Please help...
I have created a text file in the first Activity of my App, which is all working fine.
In My next Activity, i want to append to the text file.
But when i try to append the catch throws up the following error..
mnt/scard/PatRecords/testfile.txt contains a file seperator
And nothing is added to the file.
my code for appending is..
try {
File directory = new File
(Environment.getExternalStorageDirectory().getPath()+"/PatRecords");
FileOutputStream fOut = openFileOutput(directory.getPath()+"/"+FileName$, MODE_APPEND);
OutputStreamWriter OutWriter = new OutputStreamWriter(fOut);
OutWriter.write(TestNo$+"\n");
OutWriter.write(Date$+"\n");
OutWriter.close();
fOut.close();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
Error=1;
}//End of try/catch
I have tried removing the seperators etc but still doesn't work, and as far as i can see
the path shown in the catch error is correct...?
openFileOutput() is for opening files in your program data folder, which is located on the internal memory, you may only supply the file name, not the path, hence the complain mnt/scard/PatRecords/testfile.txt contains a file separator.
if you want to open files on the SD card, you have to use FileOutputStream() or something like that:
File of = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();

Where should I store a file in Android?

I have an application that creates a configuration file and a log file. I stored these in the external storage, but when I try it in my android emulator it doesn't work because the external storage isn't writable. If this happens, where should I store the files?
This is my code:
private void createConfigurationFile(){
File ssConfigDirectory =
new File(Environment.getExternalStorageDirectory()+"/MyApp/config/");
File file = new File(ssConfigDirectory, mUsername+".cfg");
if(!file.exists()){
try{
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)){
ssConfigDirectory = new File("PATH_WHERE_I_SHOULD_STORE_IT");
}
File ssLogDirectory = new File(ssConfigDirectory+"/SweetSyncal/log/");
ssLogDirectory.mkdirs();
ssConfigDirectory.mkdirs();
File outputFile = new File(ssConfigDirectory, mUsername+".cfg");
FileOutputStream fOut = new FileOutputStream(outputFile);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
writeFile(osw);
osw.flush();
osw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
If the file isn't too big you can save it in the device's Internal Storage.
To access the Internal Storage you can use the following method:
FileOutputStream openFileOutput (String name, int mode)
(You need an instance of Context to use it)
Example:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
As to why the code you provided is not working then there are two possibilities:
You forgot to add the required permission (WRITE_EXTERNAL_STORAGE).
You'r emulator doesn't have an SD card enabled. Assuming you are using Eclipse you can enabled it in the AVD Manager. Just edit your AVD instance and type in the size of the SD card in the appropriate field. You should also add a hardware feature called SD Card Support and set it to TRUE.
There is a great article in the official Developer Guide which will tell you everything you need to know about storage in Android.
You can read it HERE

Categories

Resources