creating a xml in android - android

I want to create a new xml file in android.my code is like below.It didn't work
File newxmlfile = new File("C:/Users/yunus.oksuz/Desktop/xmlFile.xml");
try
{
newxmlfile.createNewFile();
Toast msg = Toast.makeText(MainActivity.this, "file was created", Toast.LENGTH_LONG);
msg.show();
}
catch(IOException e)
{
Log.e("IOException", "exception in createNewFile() method");
}

First thing to know, you cannot even think about having a file in your PC and trying to refer it to your Android application project. They both are totally different.
let me explain the reason.
Consider you have the app installed in a phone, how do you think you can simply access your PC to which you are no more connected. This is not a regular java project you are working on. Android is totally different.
I will tell how it works. Open a emulator and wait for it to load. now,
Go to DDMs-> File Explorer
You will be able to see a folder by name "mnt" or "sdcard". Click on it. And now on the top right corner you will have three icons , one to push a file to sdcard and one to delete from sdcard and also one to pull a file from sdcard.
So click on the push a file icon and select your file and copy it to sdcard.
Now you have your file in sdcard. here you have to learn how to read a file from sdcard.
And I am sure there are so many examples available on internet on how you can read a file from sdcard.
Hope this will help you to get it started.

Here's how you can create your xml file.
File f = new File(Environment.getExternalStorageDirectory() + "/yourxmlfilename.xml");
// If file does not exist, clear log count
if(!f.exists())
f.createNewFile();

Related

invisible file on external storage of android device from nautilus

I'm sorry to take your time, so I'll try to make it short. Before all, I apologize if my English is wrong or if I'm making dumb mistake, English is not my mother tong. I'm sorry if the answer is silly I'm still not experimented with the making of android apps. I'm making a android application and I'm trying to store some files on a public folder on the external storage. I'm having an issue and I have no idea what might be happening.
The issue is so :
I have a file in the 'download' directory. Then I'm moving the file to my public folder, then I rename it (all this through code). Everything seems to happens fine, the file is correctly moved, is readable and writeable. The file is then visible through app like 'File commander'. Although, when I connect my phone with my computer the file is nowhere to be seen with nautilus (ubuntu folder app), even with the terminal (sudo ls -a doesn't change a thing).
String path = (Environment.getExternalStoragePublicDirectory("myAppFolder").getAbsolutePath() + "/inside_folder/" + fileToMove.getName())
File folder = new File(path);
folder.mkdir();
File thatFile = new File (folder.getAbsolutePath() + "/downloading");
if (thatFile.exist()) thatFile.delete();
try {
FileInputStream instream = new FileInputStream(fileToMove);
FileOutputStream outstream = new FileOutputStream(thatFile);
FileChannel inChannel = instream.getChannel();
FileChannel outChannel = outstream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
instream.close();
outstream.close();
outChannel.close();
inChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
thatFile.renameTo(new File(folder.getAbsolutePath + "/coucou"));
For some reason, if the file is rename with 'file commander', the file will now be visible through nautilus. Also, if no other file than the one invisible named 'coucou' then, I will have no way to explore to myAppFolder/inside_folder/imafile/coucou , myAppFolder (and so inside_folder) being unreachable. How can I change that ? How can I make it visible without File Commander. Also, I wonder how can a file be invisible ... anyway thank you for your time and help :-).
edit: Rebooting the device clear the issue. Yet, if new file are created, the won't appear until new reboot (or for now it seems so). I don't want the user to do so each time. Does anyone know how to fix that ? Thank you :).

Android app generate

I'm having a little problem with my android app.
My app generates a .html file when a "export button" is pressed.
But I can't see the file in my pc or in the Android's Download app. I can only see it in Astro file manager.
That's how I generate and saved my file .
String string = "Hello World"
String filename = "/sdcard/Download/teste.html";
FileOutputStream outputStream;
try {
File file = new File(filename);
boolean newFile = file.createNewFile();
if(!newFile){ //if the file exists I delete it and generate a new file
file.delete();
newFile=file.createNewFile();
}
Context context=getActivity();
FileOutputStream fOut = new FileOutputStream(file,true);
// Write the string to the file
fOut.write(string.getBytes());
/* ensure that everything is
* really written out and close */
fOut.flush();
fOut.close();
}
catch (Exception e) {
e.printStackTrace();
}
I suppose there is a way to visualize this file without the Astro app but I can't find how do this, if someone can help I'll be grateful.
Thanks
First, never hardcode paths. Your path will be wrong on some Android devices. Please use the proper methods on Environment (e.g., getExternalStoragePublicDirectory()) or Context (e.g., getExternalFilesDir()) to get the roots under which you can safely place files.
Beyond that, files that you write to external storage will not be visible to PCs until that file is indexed by MediaScannerConnection, and even then it might require the user to perform some sort of "reload" or "refresh" operation in their file browser to see it.
I have another blog post with more about external storage which may be of use to you.

File folder on Eclipse Android

I would like to make a xml file that will be modified during the execution of the application and i want to keep it after i close it for the next time i open it.
The first problem is that i don't know where do i have to put the file in the package explorer on Eclipse.
If i put the file on res/raw/ folder i could just read the file, but i can't modify.
I'm working with Jdom2.
The file is a score table for a game that will be modified every time the player finish a game.
That's the code i actually have to read the xml file stored on res/raw
try
{
puntf = getResources().openRawResource(R.raw.punt);
} catch (Exception ex)
{
Log.e("Ficheros", "Error");
}
And that's the code i actually have to modify the xml file(with Jdom2). But of course, that is wrong.
public void escritura()
{
try
{
xmlOutput.output(puntu, new FileOutputStream("punt.xml"));
}
catch (Exception e) {
e.printStackTrace();
}
}
Thanks a lot for your answers.
If you want to save a file and modify it programmatically I would suggest you to store it in this path:
/data/data/com.yourpackage.app/punt.xml
I have never worked with Jdom2, but you can have access to it by adding these lines of code:
File puntFile= new File("data/data/com.yourpackage.app/punt.xml");
FileOutputStream fos = new FileOutputStream(puntFile);
xmlOutput.output(puntu, fos);
You can also see the file in DDMS in file explorer. Just follow that path.
I can't understand if you want to save and edit the file during the process of your application or just save it somewhere before the app starts and edit it afterwords. If so, please give more details about that.
Hope I helped...
You should read uo on storage options on Andriod: THere's an article on this.... Use the resulting input and output streams for JDOM.

Outputting a txt file to a folder in internal memory in android

I am in need to create a text file where the input is taken from the user.
I am trying to save the txt file in a folder(folder should be created even if it is not there)
My code is as follows,
FileOutputStream fileos = null;
if (FreeMemory() != 0) {
FileOutputStream fos=null;
try {
File path=new File(getFilesDir(),"sri");
if(!path.exists()){
path.mkdir();
}
File mypath=new File(path,"myfile.txt");
if (!mypath.exists()) {
fos = new FileOutputStream( mypath);
String text="Write Hii";
fos.write(text.getBytes());
fos.close();
}
}catch(Exception e){
}
}
The "path" variable gives this path: "/data/data/com.example.gm/files/sri"
I navigated to this path in my device: Android-->Data-->com.example.gm-->files-->
but the folder "sri" is not created and even the file also. Am I navigating to th ecorrect path ? I am not able to find out the file in the device.
I searched for the folder by installing "Astro File Manager" app too. But, couldn't find it. I am not getting any Exception when writing to the folder. The code must be correct. But where is the folder and file? Please anyone help me in solving this.
I want to save the txt file in internal memory of my device.
Whats wrong with my code? Please suggest me the solution.
I have gone through many trails and finally approached stackoverflow.
Thanks for any help!!
The data in the internal storage is not accessible out side the application owning it, so any third party application like file browser or Astro File Manager will not be able to see the files. The application with root access will only be able to access the files in the internal memory. I believe you should programmatically verify if the file is created. getFilesDir() will give the internal memory location allocated to that app.
Have you give the permission in manifiest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Check if a file has been written to in Android

I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).

Categories

Resources