Android Java — Deserializing a file on the Android platform

ε祈祈猫儿з 提交于 2020-01-13 19:36:13

问题


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

回答1:


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



回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/5795410/android-java-deserializing-a-file-on-the-android-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!