2017-09-05 4 views
-1

プロジェクトにアセットファイルがあります。ファイルが含まれています。htmlコードです。これらのコードをベローコードの文字列で取得したいので、助けてください。 アセットファイルをBellowのStringコードアクティビティクラス以外のアセットファイルを読んでください

public class StartPage { 

     final String FILENAME = data(); 

    private static final String HEAD_1 = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\">" 
      + "<head>" 
      + "<meta content=\"en-us\" http-equiv=\"Content-Language\" />" 
      + "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />" 
      + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">" 
      + "<title>"; 


    @NonNull 
    public static File getStartPageFile(@NonNull Application application) { 
     return new File(application.getFilesDir(), FILENAME); 
    } 

    @NonNull private final String mTitle; 

    @Inject Application mApp; 
    @Inject SearchEngineProvider mSearchEngineProvider; 

    public StartPage() { 
     BrowserApp.getAppComponent().inject(this); 
     mTitle = mApp.getString(R.string.home); 
    } 

    @NonNull 
    public Single<String> getHomepage() { 
     return Single.create(new SingleAction<String>() { 
      @Override 
      public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) { 

       StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle); 

       BaseSearchEngine currentSearchEngine = mSearchEngineProvider.getCurrentSearchEngine(); 

       String icon = currentSearchEngine.getIconUrl(); 
       String searchUrl = currentSearchEngine.getQueryUrl(); 

       homepageBuilder.append(icon); 

       homepageBuilder.append(searchUrl); 


       File homepage = getStartPageFile(mApp); 
       FileWriter hWriter = null; 
       try { 
        //noinspection IOResourceOpenedButNotSafelyClosed 
        hWriter = new FileWriter(homepage, false); 
        hWriter.write(homepageBuilder.toString()); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        Utils.close(hWriter); 
       } 

       subscriber.onItem(Constants.FILE + homepage); 

      } 
     }); 
    private String data(){ 
     InputStream inputStream = getAsseet().openRawResource(R.raw.database); 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

     int i; 
     try { 
      i = inputStream.read(); 
      while (i != -1) 
      { 
       byteArrayOutputStream.write(i); 
       i = inputStream.read(); 
      } 
      inputStream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return byteArrayOutputStream.toString(); 
    } 
} 

} 

私はそう おかげ オスマン

+0

質問の本文に表示されるエラーやスタックトレースを表示します。 – jdv

+0

ここでは挿入コードのソルーションを得ることはできませんが、アクティビティクラスを挿入すると同じコードになります。 –

+0

コードをデバッグしたい人はいません。このコードに関する特定の質問がある場合は、そのコードを明確にし、エラーやスタックトレースなどの関連する結果を提供する必要があります。 – jdv

答えて

0

getAssets()をコードを書くことで私を助けてくださいアセットファイルを読むためにここにしようとしたが、ここで(メソッドgetAssetを()を解決することはできません)エラーを与えるには、クラスContextの方法であり、 。アクティビティでは、this.getAssets()と処理され、アクティビティはサブクラスContextであるため、getAssets()と呼び出すことができます。そのため、Activtyに電話することができます。あなたのクラスはContextのサブクラスではないので、あなたのクラスにgetAssets()またはthis.getAssets()を呼び出すことはできません。そのようなメソッドはないからです。

これでgetAssets()に電話するには、Contextを取得する必要があります。 Application mApp;のインスタンスがあり、Contextのサブクラスで、Activtyのサブクラスです。電話することができますmApp.getAssets()

+0

私は助けのために完全なコードを書いてください –

+0

@OsmanGani、申し訳ありませんが、なぜコードを書かないのですか?なぜ私はそれを書くべきですか?最後にあなたのコードです... –

0

以下のコードを使用してください。これがうまくいくことを願って!

String convertedString = loadJSONFromAsset(context,"myfile.html"); 
Log.d("converstion" ,"converted string" + convertedString); // it will print the entire file as string. 

public String loadJSONFromAsset(Context context,String fileName) { 
     String json = null; 
     try { 
      InputStream is = context.getAssets().open("foldername/" + fileName); 
      int size = is.available(); 
      byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 
      json = new String(buffer, "UTF-8"); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
     return json; 
    } 
+0

助けてくれてありがとうございました。ここではエラーが発生しましたString transformedString = loadJSONFromAsset(context、 "myfile.html"); Cant Resolove Symbolコンテキスト –

+0

アクティビティやフラグメントコンテキストを渡す必要があります。 getApplicationContext()やgetActivity()などがあります。 – Moorthy

+0

私のコードをここにもう一度見てください。アクティビティクラスは何ですか?ここでコンテキストを使用します –

関連する問題