Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用assetmanager的android Java.io.file_Java_Android - Fatal编程技术网

使用assetmanager的android Java.io.file

使用assetmanager的android Java.io.file,java,android,Java,Android,如何将此代码转换为Android POSModel model = new POSModelLoader() .load(new File("en-pos-maxent.bin")); 我的onCreate上有这个代码 final AssetManager assetManager = getAssets(); final InputStream inputStream = null; 然后,我尝试通过此代码访问我的资产 inputStream = assetManager.o

如何将此代码转换为Android

 POSModel model = new POSModelLoader()  
    .load(new File("en-pos-maxent.bin"));
我的onCreate上有这个代码

final AssetManager assetManager = getAssets();
final InputStream inputStream = null;
然后,我尝试通过此代码访问我的资产

inputStream = assetManager.open("en-pos-maxent.bin");
然后我尝试将inputStream传递给代码新文件(inputStream)

不确定如何将bin文件传递给模型。 我不知道如何将inputstream用于java.io.File参数

编辑:


尝试以下方法:

private  File getFileFromAsset(){
        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        OutputStream outputStream =null;
        File file =null;

            try {
                inputStream = assetManager.open("en-pos-maxent.bin");
                file = new File(getCacheDir(), "en-pos-maxent.bin");
                outputStream = new FileOutputStream(file);

                    byte[] buffer = new byte[4 * 1024]; // or other buffer size
                    int read;

                    while ((read = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, read);
                    }
                outputStream.flush();

            } catch (Exception e) {
                e.printStackTrace(); // handle exception, define IOException and others

        } finally {
            try {
                if(outputStream!=null)
                    outputStream.close();
                if(inputStream!=null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }}
            return file;

    }

然后我尝试将inputStream传递给代码新文件(inputStream)。不可能的。但请解释一下你为什么要这么做。它完全不清楚您想对资产中的“文件”执行什么操作。因此您的模型只能从文件对象加载信息。不是来自输入流。那是你的问题
.load(inputstream)
不可用?我正在尝试从我的资源访问.bin文件,以便从库中初始化函数。它需要“en pos maxent.bin”文件。因此,我尝试了使用assetmanager访问资产的常规方法,然后将其分配给inputStream。然后我将inputStream传入了.load(新文件(inputsream)),但它不起作用。您没有告诉任何新的内容。我们已经知道了。你只是重复你自己。你没有提供我要的信息。我也想知道你为什么对契丹的回答没有反应。我试着用他的建议,但没有用。我真的不知道。