2013-12-17 3 views
2

폴더,이 문제를 해결 도와주세요.파일 액세스가

+0

java 클래스 –

+4

의 구문 분석 함수에서이 코드 줄을 사용하고 있습니다. getAssets()가 null을 반환하는 것으로 보입니다. – Steve

+0

자산 폴더를 제대로 만들었습니까? 내 대답은 여기 좀 봐 : http://stackoverflow.com/a/20501733/1208581 – sulai

답변

0
private void readFiles() { 
      try 
      { 
      AssetManager assetManager = getAssets(); 
      InputStream inputStream; 
      inputStream = assetManager.open("webpages/how-to-use.html"); 

      String detail=loadTextFile(inputStream); 
      webView.loadData(detail, "text/html", "utf-8"); 

      } 
      catch(Exception e){ 
       Log.e("Exception ","======>"+e.toString()); 
      } 
     } 

    public String loadTextFile(InputStream inputStream) throws IOException { 
     ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
     byte[] bytes = new byte[4096]; 
     int len = 0; 
     while ((len = inputStream.read(bytes)) > 0) 
      byteStream.write(bytes, 0, len); 
     return new String(byteStream.toByteArray(), "UTF8"); 
     } 

희망이 있습니다. 감사합니다. !!

1

사용 Context 변수

AssetManager mngr = myContext.getAssets(); 
InputStream is = mngr.open("errorMapperConfig.xml"); 
0

getAssetes();에이 클래스에 주요 활동의 맥락을 통과해야합니다. 이 TIH 코드를 교체, 그리고

Context context = getApplicationContext(); 

: 활동과 클래스의 onCreate() 방법에 아래의 코드를 넣어

InputStream is = context.getAssets().open("errorMapperConfig.xml"); 

하면 내부 errorMapperConfig.xml라는 이름의 파일이 필요 잊지 마세요 프로젝트의 루트에 assets 폴더가 있어야합니다.

getApplicationContext()의 설명서를 참조하십시오.

관련 문제