wood burning stoves*
The moose likes Android and the fly likes reading and writeing = save game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "reading and writeing = save game" Watch "reading and writeing = save game" New topic
Author

reading and writeing = save game

Anton Sigurdsson
Greenhorn

Joined: Sep 29, 2011
Posts: 13
Hallo, I'm trying to read and write a file.
If any one haves experience in making a save game futures. I love to get any tips you have to give. I'm so green.
I made this and it is not working. I thing a mist something.

and i only need WRITE_EXTERNAL_STORAGE right? :p

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Environment;

public class Save {

private static String state;
static boolean canW, canR;
static File path = null;
static File file = null;

private static void checkState() {

state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {

Variables.text1 = "Can Write = true";
Variables.text2 = "Can Read = true";

canW = canR = true;
} else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {

Variables.text1 = "Can Write = true";
Variables.text2 = "Can Read = false";
canW = false;
canR = true;
} else {
Variables.text1 = "Can Write = false";
Variables.text2 = "Can Read = false";
canW = canR = false;

}
}

public static void save() {

String f = "car";

file = new File(path, f + ".png");

checkState();
if (canW== canR == true){

path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

path.mkdirs();

try {
InputStream is = StartGame.ourView.getResources().openRawResource(R.raw.car);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte [is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
Variables.text3 = "Image Saved";

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


}
}
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

"Not working" can mean a lot of things. Have you gone through LogCat to find any error messages? What behavior do you get that don't expect?


Steve
Anton Sigurdsson
Greenhorn

Joined: Sep 29, 2011
Posts: 13
I thing. The code is missing something!
When I read and wrote the image.
I get massage it saved on the emulator.
Bat no image in image directory
And on my phone noting happened no image save.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21


It seems to me that you use the path variable before you assign it a value. It isn't clear where the image would be saved when the path provided is null (it doesn't appear to cause a NullPointerException, but nor does it say what the location is). Try to put the assignment to path before you create the file.
Anton Sigurdsson
Greenhorn

Joined: Sep 29, 2011
Posts: 13
Thanks. I switch the two lines and it worked.
Its a classic, what came first chicken or the egg?

It looks like this now.
And it works.

Path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

line over the: file = new File(path, f + ".png");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading and writeing = save game
 
Similar Threads
program to transfer files from one server to another and displaying progress bar for each transfer
Threads and I/O stream
copy a any type of file
Code review
sms gateway