开发者

Android Java -- Deserializing a file on the Android platform

I have a jave program that serializes files that are stored and read later. So I take the serialized files and try to read them in on my Android phone (working in Eclipse) using the exact same code I used in Java SE:


FileInputStream fis = null;
try {
    开发者_开发知识库fis = new FileInputStream("iwastedahalfhouronthis.ser");
   } catch (FileNotFoundException ex) {
}

FileNotFoundException thrown. So ok, its probably not in the right place. So I place the file in every possible folder within the Eclipse project and try loading from those locations. No luck. Make sure the file name is correct, no luck. Use the fully qualified name given by eclipse: "/pleasehelp/src/com/imlosingit/iwastedahalfhouronthis.ser". No luck. Passed a file object into the FileInputStream. No luck. This was so trivially easy on Java. What's going on here?

-----------EDIT SOLUTION--------------------


        Data data = null; //object to be deserialized
        InputStream is = null;
        ObjectInputStream ois=null;
        AssetManager assets = getAssets();
        is = assets.open("fff.ser");
        ois = new ObjectInputStream(is);
        data = (com.data.Data) ois.readObject();


Check out some of the getters in Context. Try saving the files to the cache folder, or if they're going to be large files, you may want to try an external directory (e.g. SD Card).

File dir = getCacheDir();
//do stuff, saving the file in this directory

FileInputStream fis = null;
try {
    fis = new FileInputStream(new File(dir, "iwastedhalfhouronthis.ser"));
} catch (FileNotFoundException ex) {
}

EDIT: After reading your comment, I'd suggest storing your serialized files under /assets. Then you can use AssetManager to retrieve these:

AssetManager assets = getAssets();
AssetFileDescriptor afd = assets.openFd("iwastedhalfhouronthis.ser");
FileInputStream fis = afd.createInputStream();

EDIT: One more thing to try. Put the .ser file in a new folder called /raw (you'll have to create it manually), and try this:

Resources res = getResources();
//might need "R.raw.iwastedhalfhouronthis.ser" but I don't think so.
AssetFileDescriptor afd = res.openRawResourceFd(R.raw.iwastedhalfhouronthis);
FileInputStream fis = afd.createInputStream();


Take a look here - the serialized file should go to /data/data/APP_PACKAGE/iwastedahalfhouronthis.ser.


Put the files in the /res/assets directory. You can then use Context.getAssets() to return an AssetManager instance to handle the files in the assets directory.


I think we have the similar case where a javaSe app generates a binary file and the android has to read it.

This is how I did it. Place the file first in res/raw

 Resources res = getResources();
 InputStream is = res.openRawResource(R.raw.kasalvage);
 ObjectInputStream ois =new ObjectInputStream(is);
 //Blueprint is my custom object
 bp = (BlueprintMap)ois.readObject();
 Toast.makeText(this, bp.getTitle(), 2000).show();

I had to make a Jar library that contained the BlueprintMap class model and imported it to the projects both the Desktop app and the android app and it eliminated the ClassNotFoundException. I hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜