Include Local .json File In Eclipse Android Project
Solution 1:
You should put the file either in the /assets
or /res/raw
directory of your Android project. From there, you can retrieve it with either: Context.getResources().openRawResource(R.raw.filename)
or Context.getResources().getAssets().open("filename")
.
Solution 2:
Put the json file in assets folder, I have used this method like this
publicstatic String jsonToStringFromAssetFolder(String fileName,Context context)throws IOException {
AssetManagermanager= context.getAssets();
InputStreamfile= manager.open(fileName);
byte[] data = newbyte[file.available()];
file.read(data);
file.close();
returnnewString(data);
}
While parsing we can use the method like:
String jsondata= jsonToStringFromAssetFolder(your_filename, your_context);
jsonFileToJavaObjectsParsing(jsondata); // json data to java objects implementation
More Info: Prativa's Blog
Solution 3:
Put the file in the assets folder. You can use the AssetManager open(String fileName) to read the file.
Solution 4:
Under /assets in your project folder. If you don't have one, make it.
Solution 5:
Copy Asset to Local Storage
I had a very similar need. I had a label template file that I needed to provide a Bluetooth printer configuration so I included it in my assets directory and copied it to the internal storage for later use:
privatestaticfinalStringLABEL_TEMPLATE_FILE_NAME="RJ_4030_4x3_labels.bin";
InputStreaminputStreamOfLabelTemplate= getAssets().open( LABEL_TEMPLATE_ASSET_PATH );
labelTemplateFile = newFile( getFilesDir() + LABEL_TEMPLATE_FILE_NAME );
copyInputStreamToFile( inputStreamOfLabelTemplate, labelTemplateFile );
printer.setCustomPaper( labelTemplateFile.getAbsolutePath() );
copyInputStreamToFile Function
// Copy an InputStream to a File.//privatevoidcopyInputStreamToFile(InputStream in, File file) {
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = newbyte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Post a Comment for "Include Local .json File In Eclipse Android Project"