Having a problem writing out to a file, this code is taken directly from the android developer page and then tweaked a bit by me. Is there something i am missing? Quite new to Android development so sorry if it's something blatantly obvious.
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FileOutputStream outputStream;
String data = "hello";
File fileDir = new File("data.txt");
if (!fileDir.exists())
fileDir.mkdirs();
try {
outputStream = openFileOutput("data.txt",Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Basically, your problem is that you are trying to do it twice, once in a way that won't work, and once in a way that will, but hides the result.
File fileDir = new File("data.txt");
if (!fileDir.exists())
fileDir.mkdirs();
This would create a Java File object connected to a hypothetical file called "data.txt" located in the current working directory, which for an Android app is the root directory of the device - a place you most definitely are not allowed to write to. However, this may not obviously cause any errors, as the root directory exists so mkdirs() will do nothing, and you only create a File object, you don't actually try to create a file on "disk". Effectively this code does nothing for you - get rid of it.
Next you try something basically workable:
try {
outputStream = openFileOutput("data.txt",Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
openFileOutput() is a method of a Context (Activity or Service) which creates an output stream to write to an actual file located in the private internal storage area of your app. This is all fine and good, and normally a good choice for storing typical data. However, it is not a place that you will be able to examine when running a release app on a secured device, as neither ADB based tools nor Mass Storage or MTP access over USB have rights to it. So it's entirely possible that this code worked, but you had no way to discover that fact. If you are on an emulator, you can access this location with ADB or the DDMS browser, and if your apk is a debug one, you can use the run-as command line tool in the shell.
If you want to share the data, you might consider putting it on the External Storage instead.
Related
I have been trying for the last couple of days to find the right way of writing a file to a subdirectory of the android data folder.
Most answers i found were not clear or didn't address the problem in a right and working way so i finally decided to ask.
I am giving a user the possibility of backing up his data on my server by basically uploading the database to the server through a php script.
The database is located in
'/data/data/com.package/databases/data.db'
The problem here is when a user wants to reload a backed up database.
The application Downloads the file and should then write it (Overwriting the old one) to the /databases/data.db' file, and then reload.
I have managed to get everything to work up until where i have to write the downloaded file because FileOutputStream throws an illegal argument exception saying that i can't use path separators in the path.
I understand that FileOutputStream can only write to the first level of the data folder and not to subdirectories.
How can this be done?
If it can't be done, is there any way to set the default database path to the first level of the 'data' directory to solve this problem?
If this is a completely wrong approach to what i want to obtain i am open to critique but it would still be nice to get an answer, just for future knowing.
public void aSyncDatabaseDownloadFileFinish(PhpWrapper feed) {
if (validateServerResponse(feed.Result)) {
// Copy Database to Directory
try {
FileOutputStream fos = ctx.openFileOutput(ctx.getDatabasePath(DataDatabaseHelper.DBNAME).getAbsolutePath(),
Context.MODE_PRIVATE);
fos.write(feed.DownloadedBytes, 0, feed.DownloadedBytes.length);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else
Toast.makeText(ctx, ctx.getResources().getString(R.string.nofilesonserver), Toast.LENGTH_SHORT).show();
}
Thanks in advance.
How can this be done?
In your case, use getDatabasePath() to write a file into the standard location for databases for your app.
Regardless of your path separator issue, openFileOutput() will work with files/ directory in your internal storage, not the databases/ directory.
Ok,
I got the answer thanks to Squonk, Still can't believe that it was that simple.
For anybody having the same problem here is an example:
if (validateServerResponse(feed.Result)) {
// Copy Database to Directory
try {
// This Solved the Exception
FileOutputStream fos = new FileOutputStream(ctx.getDatabasePath(DataDatabaseHelper.DBNAME), true);
fos.write(feed.DownloadedBytes, 0, feed.DownloadedBytes.length);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else
Toast.makeText(ctx, ctx.getResources().getString(R.string.nofilesonserver), Toast.LENGTH_SHORT).show();
Thank you Squonk!
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.
I'm running some tests using Robotium on an Android application that interacts with a web-portal.
I'd like to save some information to file; for example I need to save the id of the username I created from the app and I want to make it read from Selenium to run tests on web-portal to verify a webpage for that user has been created.
Is it possible?
Could someone suggest me a solution or a work-around?
This is an example of code, but it doesn't work (I want to write to a file for example on c:\myworkspace\filename.txt a string):
public void test_write_file(){
if(!solo.searchText("HOME")){
signIn("39777555333", VALID_PASSWORD);
}
try {
String content = "This is the content to write into file";
File file = new File("filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertTrue(solo.searchText("HOME"));
}
This code should write to file on device; my goal is to write on a file on machine from which I'm launching the script; the application under test should have permission to write to memory card; but I ask how to go out from Android Environment and get my Desktop environment.
For tests I suppose you will need xml format to be saved: Create xml file and save it in internal storage android
And then you will need to copy saved file from your device, see this How to copy selected files from Android with adb pull
You could be not so lazy and search it yourself.
For reading from a file or writing to file you would have to use normal java method. There you can create a separate method to read/write, which can be called whenever needed. you can see examples here for normal text file and excel file.
First: see my question Reading XML online and Storing It (Using Java). Read the approved answers and the comments underneath that answer.
So, what my question here is: even though I've run through the process described in the linked question, and the .xml file saves to the /res/values folder in my Android App, its not showing up at all - not when I'm running the app, nor after I close the app.
Does anyone have any ideas on how to fix this so that when I generate the file, it will be available right away, even while the app is running, to read and use?
just use this code,
FileOutputStream fOut = null;
try {
fOut = this.openFileOutput("your xml file name.xml", MODE_PRIVATE);
try {
InputStream is = new FileInputStream("your source file");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fOut.write(buffer);
} catch(Exception e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
and you can see your xml file at the data/data/packagename/file folder Thnx. Hope this will help you.
I'm not 100% sure if you're running the XML parsing in Java or actually in your Android app.
If you're running in Java, be aware that your project structure isn't live in the emulator - the .apk was packaged up and installed before running. You need to use adb to push files into the emulator (or your Android device) before your app can see the file.
If you're accessing the file in the app:
If you use file access methods such as openFileOutput() it will show up in the private directory on the device, which would be /data/data//files/
However, if you're using "new File(" rather than "context.openFileOutput" then the file is wherever you put it.
The problem is this:
I make an internet connection to some url and receive an HttpResponse with an app_example.apk.
Then I want to create a file (an .apk)
in the sdcard with this data so that this downloaded application
can be installed later.
How can I convert the HttpResponse to an .apk file?
Let's clear some details:
I have to get this apk file through an internet connection to my server
I don't want to install this applications I receive on the sdcard
All of this has to be done in my code, I cannot use android market
I am currently writing to that file.
What I'm doing is converting the HttpResponse to a byte[ ],
then that byte[ ] is written to a file (an .apk) using an ObjectOutputStream.
Like this:
// byte[] appByteArray - already has the internet response converted in bytes
try {
file = new File(Environment.getExternalStorageDirectory()+"/"+appName+".apk");
file.createNewFile();
FileOutputStream stream = null;
stream = new FileOutputStream(file, false);
ObjectOutputStream objectOut =
new ObjectOutputStream(new BufferedOutputStream(stream));
objectOut.writeObject(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
In the end, the file is created
and has the received content.
When I try to install it,
through a VIEW intent (using the default installer)
I get a parse error saying that it could not find the AndroidManifest.xml.
I think that in some step along the way, the received data is being corrupted.
Do you have another method to solve this?
Many thanks
Don't use an ObjectOutputStream, byte array is serialized as Object, not written as raw data.
Are you sure that you have SD card write permission? android.permission.WRITE_EXTERNAL_STORAGE
Don't write into SD card root directory. Number of files in root dir can be limited. Instead create you app subdirectory on SD CARD.
This code works for me:
try {
String filePath = Environment.getExternalStorageDirectory()
+ "/myappdir/" + appName + ".apk";
File file = new File(filePath);
file.getParentFile().mkdirs();
file.createNewFile();
BufferedOutputStream objectOut = new BufferedOutputStream(
new FileOutputStream(file));
objectOut.write(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
This may not be the core problem, but I don't think you want to wrap stream in an ObjectOutputStream, since that is used for object serialization. It could be that it is adding extra data to the file so it can be deserialized with ObjectInputStream.
I would try pulling the apk off of the emulator (or device) and check it's MD5 versus the file on the server to make sure that the bits are being written out correctly.
Take a look at Pavel P's answer.
Also, I would note that your idea of installing the APK using the VIEW intent action does work, as I have tested this technique in the past.
However, unless the user has explicitly gone into Settings → Applications and selected "Allow non-Market applications", your installation will fail and the user will just see a screen telling them that for security reasons the installation has been blocked.
Basically you really need to rely on having fairly tech-savvy users who are willing to overlook a scary security warning and go and disable that setting.