Saving with android

I have been making a game for android and I was wondering how to save a txt file. I notice that the standard java.io doesn’t work on android. I was hoping to save it to the sd card with this code:





File root = Environment.getExternalStorageDirectory();

if (root.canWrite()){

File file = new File(root, “test.txt”);

FileWriter fw = new FileWriter(file);

BufferedWriter out = new BufferedWriter(fw);

out.write(“Hello world”);



out.close();

}

} catch (IOException e) {



}



However I need to import android.os.Enviroment which I add the android.jar from the andoridSDK. However this mess up the compile saying I have 2 manifest files.



Is there a way to just save a txt file?



Thanks

Read the manual on how to use android specific code in your application.

Where is that manual located. I am unable to find it on the jmonkey website



Thanks

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:android

Ok, if finding that info is problematic I suggest improving information gathering skills before continuing ^^ You can open the manual in the SDK using the “F1” key, its searchable and contains all the information from the wiki on this site.

This is what I do. I use it for both PC and Android versions.



main game classs, in simpleInitApp:

[java]

String Platform = JmeSystem.getPlatform().toString();

boolean isAndroid = Platform.contains("Android");



logger.log(Level.INFO, "Platform: {0}", JmeSystem.getPlatform().toString());

logger.log(Level.INFO, "isAndroid: {0}", isAndroid);



String packageName = Main.class.getPackage().getName();

logger.log(Level.INFO, "packageName: {0}", packageName);

logger.log(Level.INFO, "Starting new FileOperations");

FileOperations fileOps = new FileOperations(packageName, isAndroid);



[/java]





FileOperations.java

[java]

if (isAndroid) {

Class clazz = null;

Method method = null;

Object tmpObj = null;



try {

clazz = Class.forName("android.os.Environment");

} catch(ClassNotFoundException e) {

logger.log(Level.INFO, "Class Not Found: {0}", e.getLocalizedMessage());



method = clazz.getDeclaredMethod("getExternalStorageState");

tmpObj = method.invoke(null, (Object[]) null);



if (tmpObj == null) {

logger.log(Level.INFO, "invoke returned null");

} else {

if (tmpObj instanceof String) {

externalStorageState = (String) tmpObj;

logger.log(Level.INFO, "externalStorageState: {0}", externalStorageState);

} else {

logger.log(Level.INFO, "invoke returned non-String object: {0}", tmpObj.getClass().getName());

}

}



method = clazz.getDeclaredMethod("getExternalStorageDirectory");

tmpObj = method.invoke(null, (Object[]) null);

if (tmpObj == null) {

logger.log(Level.INFO, "invoke returned null");

} else {

if (tmpObj instanceof File) {

externalStorageDirectory = ((File) tmpObj).getAbsolutePath();

logger.log(Level.INFO, "externalStorageDirectory: {0}", externalStorageDirectory);

} else {

logger.log(Level.INFO, "invoke returned non-File object: {0}", tmpObj.getClass().getName());

}

}



} catch (Throwable e) {

logger.log(Level.INFO, "ERROR: {0}", e.getLocalizedMessage());

}



if (externalStorageDirectory.length() < 1) {

externalStorageDirectory = "/mnt/sdcard";

}

externalStorageDirectory += "/Android/data/" + packagename + "/files/";

if (externalStorageState.equals("mounted")) {

externalStorageAvailable = true;

}



} else {

externalStorageDirectory = System.getProperty("user.home") + "&quot; + packagename + "\files&quot;;

externalStorageAvailable = true;

}

logger.log(Level.INFO, "externalStorageDirectory: {0}", externalStorageDirectory);

logger.log(Level.INFO, "externalStorageAvailable: {0}", externalStorageAvailable);



if (externalStorageAvailable) {

file = new File(externalStorageDirectory);

logger.log(Level.INFO, "making directories for: {0}", file.getAbsolutePath());

boolean madeDirs = file.mkdirs();

logger.log(Level.INFO, "madeDirs: {0}", madeDirs);

} else {

logger.log(Level.SEVERE, "Failed to create directories, externalStorageAvailable: {0}", externalStorageAvailable);

}



[/java]

1 Like

Also, Don’t forget you need to add the following line to AndroidManifest.xml



[java]

<uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE”/>



[/java]