在 Eclipse Android 项目中包含本地 .json 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7109372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:51:39  来源:igfitidea点击:

Include local .json file in Eclipse Android project

javaandroideclipsejson

提问by Magnus

I have a local .json file. I don't want it to be on a server, I just want it to be included in my app. I tried to paste it directly into Eclipse in my project, but I got a FileNotFoundException, I also tried to paste it in the workspace folder in Windows Explorer/Finder and got the same exception. Where should I put it?

我有一个本地 .json 文件。我不希望它在服务器上,我只想将它包含在我的应用程序中。我试图将它直接粘贴到我的项目中的 Eclipse 中,但是我得到了一个 FileNotFoundException,我也尝试将它粘贴到 Windows Explorer/Finder 的工作区文件夹中并得到了同样的异常。我应该把它放在哪里?

Thanks!

谢谢!

回答by Erich Douglass

You should put the file either in the /assetsor /res/rawdirectory of your Android project. From there, you can retrieve it with either: Context.getResources().openRawResource(R.raw.filename)or Context.getResources().getAssets().open("filename").

您应该将该文件放在Android 项目的/assets/res/raw目录中。从那里,您可以使用:Context.getResources().openRawResource(R.raw.filename)或来检索它Context.getResources().getAssets().open("filename")

回答by LOG_TAG

Put the json file in assets folder, I have used this method like this

将json文件放在assets文件夹中,我用过这种方法

public static String jsonToStringFromAssetFolder(String fileName,Context context) throws IOException {
        AssetManager manager = context.getAssets();
        InputStream file = manager.open(fileName);

        byte[] data = new byte[file.available()];
        file.read(data);
        file.close();
        return new String(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

更多信息:Prativa 的博客

回答by Kevin

Put the file in the assets folder. You can use the AssetManageropen(String fileName) to read the file.

将文件放在资产文件夹中。您可以使用AssetManageropen(String fileName) 来读取文件。

回答by Joshua Pinter

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:

我有一个非常相似的需求。我有一个标签模板文件,需要提供蓝牙打印机配置,所以我将它包含在我的资产目录中,并将其复制到内部存储以备后用:

private static final String LABEL_TEMPLATE_FILE_NAME = "RJ_4030_4x3_labels.bin";

InputStream inputStreamOfLabelTemplate = getAssets().open( LABEL_TEMPLATE_ASSET_PATH );

labelTemplateFile = new File( getFilesDir() + LABEL_TEMPLATE_FILE_NAME );

copyInputStreamToFile( inputStreamOfLabelTemplate, labelTemplateFile );

printer.setCustomPaper( labelTemplateFile.getAbsolutePath() );

copyInputStreamToFile Function

copyInputStreamToFile 函数

// Copy an InputStream to a File.
//
private void copyInputStreamToFile(InputStream in, File file) {
    try {
        OutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while((len=in.read(buf))>0){
            out.write(buf,0,len);
        }
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

回答by colig

Under /assets in your project folder. If you don't have one, make it.

在项目文件夹中的 /assets 下。如果你没有,那就去做吧。