Android: Is it possible to create a media file using openFileOutput function - android

I had created files i.e., txt files and stored values into it using openFileOutput fn. This function creates the txt file in the data/data/app_name/files folder.. Now i am trying to save a media files like .mp3 file to that data/data/app-name folder.. Is it possible to do so?
Thanks in advance.
fOut = openFileOutput("a.txt", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
String aa="hi";
osw.write(aa);
osw.flush();
osw.close();
This is how i had written a file to the data/data/app-name folder.. Nw i am trying to write a mp3 file but the osw.write(aa) accepts string or char array..

Context.openFileOutput() (reference) creates a FileOutputStream object which is capable of writing binary data (see write() method), so the answer is Yes, it can be used to write media files.

Related

Android - Write file to assets folder

I want to download some data and store it into a file. I am aware that I cannot write to the assets dir or any other place because of the sole nature of the APK file.
I am wondering, if I create a file with something like:
FileOutputStream fileout = openFileOutput("text.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
outputWriter.write("write this string to file.");
outputWriter.close();
Since I cannot write any files to assets or any raw dir, where it will be written?
What will the location of that file be? Where will it be located in the Android file system ?
Since I cannot write any files to assets or any raw dir, where it will be written?
openFileOutput() writes to internal storage.
What will the location of that file be? Where will it be located in the Android file system ?
That can vary by device and user. It will be to the same directory that is returned via a call to getFilesDir().
You can get the path to your file created with openFileOutput like this: MainActivity.this.getFilesDir().getAbsolutePath()+"/text.txt" and you can read it using an object of FileInputStream FileInputStream fis = new FileInputStream(new File(MainActivity.this.getFilesDir().getAbsolutePath()+"/text.txt"));

How are objects created for FileOutputStream

I am a beginner in android also in Java, many times I get confused with various Java implementations in android one of them is:
In the statements
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
what is fos in the above statement? It is obviously object of fileOutputStream but why is it not implemented as
FileOutputStream fos = new FileOutputStream(FILENAME, Context.MODE_PRIVATE);
If possible give me a simple example in java/android.
openFileOutput is a method of the Context that automatically creates a file output stream to your context's data folder.
If you use the constructor of FileOutputStream, you have to pass a full file path as the first argument, while the Context takes only the file name. For files that need not be accessible via file managers, use openFileOutput for saving the file and openFileInput for reading the file again.

can't find file created in dir returned by getFilesDir()

One part of my app needs to write a data file out.
I use getFilesDir() and it gives me a path like this (after adding a file name):
/data/data/com.casadelgato.zillaconfigdroid/files/Log.1378357559316.csv
I create a PrintWriter to the file, write a bunch of stuff to it, and close it.
No errors.
The problem is that I can't find the file anywhere on my Android device.
mention the file name in the path
FileOutputStream fos = context.openFileOutput(path,
Context.MODE_PRIVATE);
write the file into the stream...and you get the file by passing the same file name
FileInputStream openFileInput = context.openFileInput(path);
read the input stream

how to create .json file and how to write json object into json file ...In android

I am using this code for creating the new file and write the json object in test.json file but I am unable to create file.Please find mistake and give the best result
One things I have also write the code for permission in manifest file..
Thanks,
String fileName = "test";
File file = new File("/data/data/packagename/test.json");
file.createNewFile();
DataOutputStream fos = new DataOutputStream(openFileOutput(fileName , Context.MODE_PRIVATE));
if(file.exists()){
fos.write(jsString.getBytes());
fos.close();
}
This will not land on C:/ anyway. At best at your SD card or some other folder, and since android is a unix based system. you should be careful on how you use "slashes"

How to ReadWrite into textfile in android?

How to read and write strings in text file in android which is kept in raw folder and also how to clear contents of that file in android?
I have done this way and my "temp.txt" file is in /raw folder. But i am not getting any output in file. Also I am not getting any error for this.
FileOutputStream fos = openFileOutput("temp", Context.MODE_APPEND);
fos.write("Example Text in a file".getBytes());
fos.flush();
fos.close();
Try this one
Try to put file int /asset folder and then do operations

Categories

Resources