I am trying to read some files and put them into a new file using the below method in Eclipse.
But I am getting a read-only file system EROFS error in eclipse at run time.
Input file names will be provided as function parameter. Files are present in res\raw folder.
SampleFile.mp3 is an empty audio file placed at the location.
public void myfun(Set s)
{
try {
FileOutputStream fos=new FileOutputStream(".\\res\raw\sampleFile.mp3",true);
FileInputStream fis;
Iterator ptr=s.iterator();
String str;
while(ptr.hasNext()!=null)
{
str=ptr.next().toString();
fis=new FileInputStream(str);
int i;
while((i=fis.read())!=-1)
{
fos.write(i);
}
fis.close();
}
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You are trying to write into the 'res/raw' folder of the current directory, but android does not work like that. Go read the Storage Options section of the Android Development Guide, and choose one that fits your needs (and use case).
Also, you want to concatenate multiple files into an mp3? Are they by any chance... mp3 files themselves that you want to play?
Related
I am writing a log data to the File in android. But I am unable to understand where this file is stored on my android device ( google nexsus 7 ). Where and How should I look for the contents of the file ?
Following is the piece of code I am using to write log data to the file.
public File AppendingLog(String LogData){
try {
File logFile = new File(Environment.getExternalStorageDirectory(),
"yourLog.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(LogData);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return logFile;
}
android.util.Log.d("myapp", logFile.toString());
On most devices, it will be in /sdcard/. The exact path will be /sdcard/yourLog.txt.
As you are using Environment.getExternalStorageDirectory(), the actual path will differ between devices. Some devices return the external sd card path, but most will return the internal sd card's path, which usually is /sdcard/.
I think answers below are what you need.
You can also download Astro File Manager App which will also give you an idea where your files go on Android System.
I need to display a PDF document inside the mobile browsers, without asking if the user wants to download it. Using Safari (iOs) I can do it perfectly, but when I try to do the same thing using the Android's (version ICS) native browser (called "Internet") the file is downloaded, and even the download doesn't seem to work.. After the download is done, the file is shown as Download unsuccessful, and I can't open it using a PDF file reader.
The same link download works fine is all others browsers whos not mobile.
I believe the problem is in some of the headers that I need to add to the response.
I'm setting the Content-Type as application/pdf and the Content-Disposition as inline;filename="report.PDF"
Is there anyone who had the same problem?
Anyone who knows a solution? :)
Thanks!
Lucas
The android browser does not have a default built in PDF support so this cannot be easily done with that.
However here are two things you could do.
1) Download and install adobe reader from the play store and then try the following code
First you create a file on the device memory and then you read the pdf data into it.
"theurl" is the url of where the pdf is located
InputStream in = null;
File dst = new File(Environment.getExternalStorageDirectory()+"/myPDF.pdf");
try {
in = theurl.openStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStream out = null;
try {
out = new FileOutputStream(dst);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
// Transfer bytes from in to out
try {
byte[] buf = new byte[100000];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent();
Uri path = Uri.fromFile(dst);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
This will download the pdf, create it in your device memory and then the intent will open it on through your application using adove reader on your device.
Another option is
2) Go to http://jaspreetchahal.org/android-how-to-open-pdf-file-within-the-browser/ and see how that guy did it
In my app there are 3 EditTexts. I want to write the content of this EditTexts to a file, but the filewrite throws a nullpointer exception. Why?
OutputStream f1; is declared globally.
BtnSave = (Button)findViewById(R.id.Button01);
BtnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
intoarray = name + "|" + number + "|" + freq + "\n";
Toast.makeText(Main.this, "" + intoarray, Toast.LENGTH_SHORT).show();
//so far so good
byte buf[] = intoarray.getBytes();
try {
f1 = new FileOutputStream("file2.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1.write(buf); //nullpointer exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Most likely
f1 = new FileOutputStream("file2.txt");
failed and since you caught the exception f1 remained null. In most cases in Android you can only create files either in your application data directory or external storage.
The way you are currently using this won't work, generally you are trying to write to internal storage, which is private to your app and must be contained within your applications directory.
The proper way to create the file stream is
fin = openFileOutput("file2.txt", Context.MODE_PRIVATE); // open for writing
fout = openFileInput("file2.txt", Context.MODE_PRIVATE); // open for reading
Which will locate the file in your storage area for your application, which is typically something like
/data/data/com.yourpackagename/files/...
You can still create directories within your applications area if you need a directory structure of course.
If you need to write to external storage that's a different process, for more information see Android Data Storage
Sorry for all you were trying help me, I asked the wrong question. I wanted to use internal storage (and it is now working). I don't know what the problem is, but the with the code below (that i have used a lot) filewrite is ok:
try {
File root = Environment.getExternalStorageDirectory();
File file = new File(root, "Data.txt");
if (root.canWrite()) {
FileWriter filewriter = new FileWriter(file, true);
BufferedWriter out = new BufferedWriterfilewriter);
out.write(intoarray);
out.close();
}
} catch (IOException e) {
Log.e("TAG", "Could not write file " + e.getMessage());
}
I would delete the topic if I could. I accept this answer to close the topic.
Thanks, anyway.
I am trying to write to an existing XML file in the /res/xml directory on an Android device. Two questions:
1) Are these files able to be overwritten?
2) If so, how can I obtain a handle to that file?
I am trying this to no avail:
FileOutputStream fos;
try {
fos = openFileOutput("/res/xml/scores.xml", Context.MODE_PRIVATE);
fos.write(writer.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UPDATE
Trying to read from the file using:
....
PriorityQueue<Score> scores = new PriorityQueue<Score>();
XmlPullParserFactory xmlFac = XmlPullParserFactory.newInstance();
XmlPullParser qXML = xmlFac.newPullParser();
File scoresFile = new File(c.getExternalFilesDir(null), "scores.xml");
InputStream is = new FileInputStream(scoresFile);
qXML.setInput(is,null);
....
Trying to write to the file using:
....
File scoresFile = new File(getExternalFilesDir(null), "scores.xml");
OutputStream os = new FileOutputStream(scoresFile);
os.write(writer.toString().getBytes());
os.flush();
os.close();
....
You need to use the external storage on the device. You can't and shouldn't be writing to the resources. Instead, copy the resource to the external storage, and then modify it there.
EDIT: You can also use your application's internal data folder to save files to, but again, you will want to copy your resource there if you want to modify it. This will allow the files to remain internal to your application.
I have problem with writing data to file in Android Emulator. In my Android Emulator in /data folder I have created MyLogs folder and give full access to it. After I run my application and it create Log.txt file and place it in /data/MyLogs folder. All is Okay. After I have run my application in second time and application try to write some information in same file, but it cant't.
I think the main reason is that then at first time my application creates file the creator is different from second time. thats why I can't write to file second time !
Who have any ideas ?
To make a file writable for more than once use Context.MODE_APPEND
Sample Code
FileOutputStream fos;
try {
fos = openFileOutput("nuzz.txt", Context.MODE_APPEND);
fos.write(string.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos = openFileOutput("nuzz.txt", Context.MODE_APPEND);
fos.write("bye".getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thanks
Deepak