2011-12-10 2 views
1

내 데이터베이스에 저장된 경로가 text1.txt이며 표시 할 텍스트가 포함되어 있습니다. 파일은 assets 폴더 인 assets/text1.txt에 있습니다.데이터베이스에 저장된 경로를 사용하여 assets 폴더에서 파일을 여는 방법은 무엇입니까?

이 파일을 열고 내용을 표시하려면 어떻게해야합니까?

if (placetext != null) { 
    try 
    { 
     InputStream textpath = getAssets().open(text); 
     //Bitmap bit = BitmapFactory.decodeStream(textpath); 
     placetext.setText(text); 
     //placetext.setText(text); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

... 그리고 에뮬레이터에보기는 파일의 내용 만 text1.txt되지 않습니다 :

코드입니다.

는 이미 해결책을 가지고

문자열 text12 = b.getString ("텍스트") 시도가 { 의 InputStream은 =에 getAssets() 개방 (text12)입니다.; // int size = is.available(); 내가 여기 추측

InputStream textpath = getAssets().open(text); 

: 텍스트 열 파일의 이름을 나타내므로 정상

  byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 

      String text= new String(buffer); 

      placetext = (TextView)findViewById(R.id.detailText2); 
      placetext.setText(text); 
      } catch (IOException e) { 
       throw new RuntimeException(e); 
      } 

답변

0

.

placetext.setText(text); 

텍스트 입력란에 매개 변수로 전달 된 텍스트를 입력하면 파일 이름이 표시됩니다.

파일의 내용을 텍스트 필드에 넣으려면 파일을 열고 읽은 다음 StringBuffer에 내용을 저장하고 StringBuffer 내용을 텍스트 필드에 넣어야합니다.

편집 : 다른 사람의 많은 중

StringBuilder text = new StringBuilder(); 
Scanner scanner = new Scanner(new FileInputStream(new File('yourfile'))); 
    try { 
     while (scanner.hasNextLine()){ 
     text.append(scanner.nextLine()); 
     } 
    } 
    finally{ 
     scanner.close(); 
    } 

} 

하나의 솔루션은 자바에있는 파일의 내용을 읽을 수 있습니다.

는 이것은 내가에/자산 폴더에 저장된 XML의 내용을 읽는 데 사용할 것입니다

+0

감사 당신의 응답을; InputStream textpath = getAssets(). open (text), text sqlite의 path를 포함하는 필드를 나타냅니다. 그것은 이미지 파일과 이미지 경로와 함께 작동하지만, text.txt에서 작동하지 않습니다 –

+0

당신을 환영합니다! :) 그게 효과가 있니? –

+0

아니요, 작동하지 않습니다. ( –

0

도움이되기를 바랍니다 :

public static String getXMLFromAssets(Context ctx, String pathToXML){ 
     InputStream rawInput; 
     //create a output stream to write the buffer into 
     ByteArrayOutputStream rawOutput = null; 
     try { 
      rawInput = ctx.getAssets().open(pathToXML); 
      //create a buffer that has the same size as the InputStream 
      byte[] buffer = new byte[rawInput.available()]; 
      //read the text file as a stream, into the buffer 
      rawInput.read(buffer); 
      rawOutput = new ByteArrayOutputStream(); 
      //write this buffer to the output stream 
      rawOutput.write(buffer); 
      //Close the Input and Output streams 
      rawOutput.close(); 
      rawInput.close(); 
     } catch (IOException e) { 
      Log.e("Error", e.toString()); 
     } 
     //return the output stream as a String 
     return rawOutput.toString(); 
} 
관련 문제