Pregunta

I am getting the file path .How to get content of file. Then I need to send to javascript. I need all data in string or stringbuilder so that I can send in javascipt.can you please tell me how to read content of file with path

 ------------------FilePath------------------/storage/sdcard0/Download/a.txt

 @Override  
 protected void onActivityResult(int requestCode, int resultCode,  
                                    Intent intent) {  

      if (requestCode == FILECHOOSER_RESULTCODE) {

            if (mUploadMessage == null)
                return;

            Uri result = intent == null || resultCode != RESULT_OK ? null
                    : intent.getData();
            if (result!=null){

                String filePath = null;

                if ("content".equals(result.getScheme())) {

                    Cursor cursor = this.getContentResolver().query(result, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                        cursor.moveToFirst();   
                        filePath = cursor.getString(0);
                        cursor.close();

                } else {


                    filePath = result.getPath();
                    System.out.println("------------------FilePath------------------"+filePath);
                    // content send to java script
                  //String msgToSend = Msg.getText().toString();
                    //  web.loadUrl("javascript:loadData(\""+msgToSend+"\")");
                    //  web.loadUrl("javascript:loadData()");
                        filePath = result.getPath();

                }

                Uri myUri = Uri.parse(filePath);
                mUploadMessage.onReceiveValue(myUri);

            } else {

                mUploadMessage.onReceiveValue(result);

            }

            mUploadMessage = null;
        }
  }  
¿Fue útil?

Solución

If you have the filePath you can try something like the following:

File file = new File(filePath);


StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {
    //Exception-handling
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top