In my app I save some files using FileOutputStream class:
FileOutputStream fos;
fos = openFileOutput("my_file", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(classToSave);
os.close();
If I upgrade my app anche the I execute:
FileInputStream fis = null;
fis = openFileInput("my_file");
ObjectInputStream is = new ObjectInputStream(fis);
myData = (MyClass) is.readObject();
is.close();
does fis is null or it contains the class that I saved before upgrade?
The file itself will be still there after the upgrade. If you can retrieve the serialised object depends on which changes you made to MyClass in the source code.
Related
I made this code to write on a sdcard, now how can I transform it to write into the internal memory ?
//private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/liste.txt"; Path used to write on sdcard
try{
File file = new File(path);
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}
In android each application has its own folder under /data/data/package/
this dir is only accessable by your app and the root if the device is rooted
to access this dir and read/write to it, you can use:
getApplicationContext().openFileOutput(name, mode);
and
getApplicationContext().openFileInput(name);
more about this here : Docs
USe this code:
FileOutputStream fos;
fos = context.openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(data.getBytes()); //write to application top level directory for immediate sending
fos.close();
try{
File file = new File("/data/data/com.example.packagename/files/");
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}
or
//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext();
filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath);
I am saving out and loading mixed data types. I either have the saving part wrong or the loading part wrong. I am using buffered serial save and load method.
Variable lastFetchDate is defined as a string and initialized as "00/00/00".
It throws an error when reloading the data after it has been saved. What is wrong? I would have thought the opposite to writeBytes would be readBytes for a string.
Saving is as follows:
FileOutputStream fos = new FileOutputStream("userPrefs.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeBytes(lastFetchDate);
// I close all streams
Loading is as follows:
FileInputStream fis = new FileInputStream("userPrefs.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
lastFetchDate=(String)ois.readObject(); //<<<<< Error thrown here
// I close all streams
You have written string as byte[] so need to read as byte[]
byte [] bString = new byte[lastFetchDate.length()*2];
ois.readFully(bString, 0, bString.length);
Or if you write as Object using writeObject method then you can read as object,
oos.writeObject(lastFetchDate);
If I have a file in my resources (i.e. in res/raw for example), how do I open it for reading into an input stream? The file might be anything: a text file, a class seriaisation, etc.
On the PC I would use:
MyClass x = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
fis = new FileInputStream("/home/me/Desktop/A.dat");
ois = new ObjectInputStream(fis);
x = (MyClass)ois.readObject();
You can open files from resources in the following way:
BufferedInputStream bis = new BufferedInputStream(getResources().openRawResource(R.raw.A));
I want to write a serializable object to file in internal memory. Then, I want to load that object back from that file later. How could I do this in Android?
First of all your object must implement Serializable. Don't forget to add a serialVersionUID on the serializable class.
Then if you don't want to save specific field of the object mark it as transient.
Be sure all fields are serializable.
Next create a file in the internal memory and create an ObjectOutputStream to save your object. If you want to save in a specific folder you can create a path like this:
File path=new File(getFilesDir(),"myobjects");
path.mkdir();
Then you can use that path to save your object:
File filePath =new File(path, "filename");
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
Reading is similar:
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
MyObjectClass myObject = (MyObjectClass ) in.readObject();
in.close();
I am trying to download a set of .jpg's from Amazon S3 and store them to the internal storage (so they can't be copied by a malicious user). I have gotten this far but now I am stuck. I have found multiple questions that deal with bitmaps or arrays but nothing about storing an image and then accessing it later. Anyone know where I go from here?
String itemName = iconNames.getString(iconNames.getColumnIndexOrThrow(DbAdapter.KEY_ICON));
itemName = itemName + ".jpg";
GetObjectRequest getObject = new GetObjectRequest(bucket, itemName);
S3Object icon = mS3Client.getObject(getObject);
InputStream input = icon.getObjectContent();
I have looked here in the dev guide and it gives the following code
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
But this is for storing a string, not an image...
You have to copy InputStream to OutputStream. Something like this:
InputStream input = icon.getObjectContent();
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = input.read(buf)) > 0) {
fos.write(buf, 0, len);
}
input.close();
fos.close();
You could do something like the following.
Bitmap bitmapPicture = someBitmap
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "tmp.png");
fOut = new FileOutputStream(file);
bitmapPicture.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());