0

I have used he Download Manager Class to download a text file from the server and stored it in the common external storage. Then I want to show the text file, may be using the listView class. I have searched the web and found that there are many examples showing how to display a text file from the App's resources. However, how can I show the file which is stored in a specific file path?

Thank you very much.

eepty
  • 716
  • 3
  • 10
  • 28

3 Answers3

0

In your layout you'll need something to display the text. A TextView is the obvious choice. So you'll have something like this:

<TextView 
android:id="@+id/text_view" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/>

And your code will look like this:

//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
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');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text);

Also, put external storage read permission to manifest file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

OR You can also use default intent to do the JOB!

File txtfile = new File("/sdcard/some_file.txt");
Intent i = new Intent();
i.setDataAndType(Uri.fromFile(txtfile), "text/plain");
startActivity(i);

--

U.Swap
  • 1,921
  • 4
  • 23
  • 41
0

Try below code.

  private void openFile() {

     File file = new File("file_path");
     Uri path = Uri.fromFile(file);
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     intent.setAction(Intent.ACTION_VIEW);
     intent.setData(path);
     intent.setType("text/plain");
     try {
         startActivity(intent);
     } catch (ActivityNotFoundException e) {
         Toast.makeText(getActivity(), "No application found",
                Toast.LENGTH_SHORT).show();
     }
 }
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
0

You can open file from sd card by using code given below in external application which compatible to file extension,

   public void openDocument(String fileName) {

    File file = new File(Environment.getExternalStorageDirectory(), DATA_DIRECTORY + "/" + fileName);
    Uri fileUri = Uri.fromFile(file);
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUri.toString());
    String mimetype =  MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    if (extension.equalsIgnoreCase("") || mimetype == null) {
        // if there is no extension or there is no definite mimetype, still try to open the file
        intent.setDataAndType(fileUri, "application/*");
    } else {
        intent.setDataAndType(fileUri, mimetype);
    }
    // custom message for the intent
    startActivity(Intent.createChooser(intent, "Choose an Application:"));


}
R_K
  • 803
  • 1
  • 7
  • 18