2011-05-15 3 views
1

안녕하세요. 내 서버에서 텍스트 파일을 다운로드하는 응용 프로그램이 있습니다. 그러면 내가하고 싶은 것은 텍스트 파일에 의해 동적으로 작성된 메시지 만 보여주는 대화 상자입니다. 예를 들어 내가 다음안드로이드는 대화 상자에 텍스트 파일을 읽습니다.

지금 나는이

같은 간단한 대화 상자를 만들려면 몇 가지 메시지를 포함하고 내 서버에서 다운로드 내 텍스트 파일과 텍스트 파일이 있다고 가정 해
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage(<text from file goes here>) 
     .setCancelable(true) 
     .setPositiveButton("Okay", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel 
      } 
     }).show(); 
,210

메시지가 크게 어떤 도움을 주셔서 감사 또는 제안

을 주시면 감사하겠습니다으로 내가 텍스트 파일을 읽은 후 내 대화 상자에 쓰기 수있는 방법에 어떤 도움
+0

어떻게 exaclty에 텍스트 파일을 가져 옵니까? HTTP를 통해 –

+0

내가 생각합니다. –

+0

그래서 문제가 무엇입니까? 서버에서 읽을 수 있다면 로컬 파일에서 읽는 법을 모릅니다. 그렇습니까? –

답변

1

내 문제를 파악 최대 종단이

처럼했다

대화

AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); 
try { 
    builder.setMessage(readFile(context.getFilesDir().getAbsolutePath() + "/filename")) 
      .setCancelable(true) 
      .setPositiveButton("Okay", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }).show(); 
} catch (IOException e) { 
    Toast.makeText(Activity.this, "Error", Toast.LENGTH_SHORT).show(); 
    e.printStackTrace(); 
} 

방법

private static String readFile(String path) throws IOException { 
     FileInputStream stream = new FileInputStream(new File(path)); 
     try { 
     FileChannel fc = stream.getChannel(); 
     MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 
     return Charset.defaultCharset().decode(bb).toString(); 
     } 
     finally { 
     stream.close(); 
     } 
    } 

도와 주셔서 감사합니다.

관련 문제