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();
}
}
}
}