Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading and writeing = save game

 
Anton Sigurdsson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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?
 
Anton Sigurdsson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic