2017-03-19 6 views
-2

このコードをAndroid用に変換するにはどうすればよいですか?AndroidManager for assetmanagerを使用したJava.io.ファイル

POSModel model = new POSModelLoader() 
    .load(new File("en-pos-maxent.bin")); 

私は、私は、このコードによって

inputStream = assetManager.open("en-pos-maxent.bin"); 

を自分の資産にアクセスしようとする私のonCreate

final AssetManager assetManager = getAssets(); 
final InputStream inputStream = null; 

上でこのコードを持って、私は(コード新しいファイルへのInputStreamを渡そうとしましたinputStream);

binファイルをモデルに渡す方法がわかりません。 java.io.Fileパラメータにinputstreamをどのように使用するのか分かりません。

EDIT: Here's what I did

Here's the original code

+0

'私は、コードの新しいファイル(InputStreamの)へのInputStreamを渡そうとしました;'。不可能。しかし、あなたが何をしようとする理由を説明してください。あなたの資産であなたの「ファイル」をどうしたいのかは完全にはっきりしていません。 – greenapps

+0

あなたのモデルは、Fileオブジェクトからのみ情報を読み込むことができます。 InputStreamからではありません。それはあなたの問題だ。 '.load(inputstream)'は利用できません。 – greenapps

+0

資産から.binファイルにアクセスしようとしていますが、ライブラリから関数を初期化できます。 "en-pos-maxent.bin"ファイルが必要です。私はassetmanagerを使ってアセットにアクセスし、それをinputStreamに割り当ててみました。その後、私は.load(new File(inputsStream))でinputstreamを渡しましたが、うまくいきませんでした。 – Kon

答えて

0

法の下で試してみてください:

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; 

    } 
関連する問題