Как прочитать выбранный текстовый файл с SD-карты на Android

Я новичок в разработке для Android, и мне нужна ваша помощь. Я зацикливался на темах, которые похожи для моего развития, но не помогают мне. Пока я создаю функции, которые получают файлы с моей SD-карты и показывают мне их список. Это работает

это код для получения путей на SD-карте:

package com.seminarskirad;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class LoadActivity extends ListActivity{

     private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; }
     private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
     private List<String> directoryEntries = new ArrayList<String>();
     private File currentDirectory = new File("/");

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Browse(Environment.getExternalStorageDirectory());

        }

        private void upOneLevel(){
                if(this.currentDirectory.getParent() != null)
                        this.Browse(this.currentDirectory.getParentFile());
        }

        private void Browse(final File aDirectory){
                if (aDirectory.isDirectory()){
                        this.currentDirectory = aDirectory;
                        fill(aDirectory.listFiles());

              }
        }

        private void fill(File[] files) {
                this.directoryEntries.clear();
                if(this.currentDirectory.getParent() != null)
                        this.directoryEntries.add("..");

                switch(this.displayMode){
                        case ABSOLUTE:
                                for (File file : files){
                                        this.directoryEntries.add(file.getPath());
                                }
                                break;
                        case RELATIVE: // On relative Mode, we have to add the current-path to the beginning
                                int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                                for (File file : files){
                                        this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
                                }
                                break;
                }

        ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.load, this.directoryEntries);
        this.setListAdapter(directoryList);
        }

        protected void onListItemClick(ListView l, View v, int position, long id) {
                int selectionRowID = position;
                String selectedFileString = this.directoryEntries.get(selectionRowID);
                if(selectedFileString.equals("..")){
                        this.upOneLevel();
                }else if(selectedFileString.equals()){   /// what to write here ???
                        this.readFile();    ///what to write here???
                } else {
                        File clickedFile = null;
                        switch(this.displayMode){
                                case RELATIVE:
                                        clickedFile = new File(this.currentDirectory.getAbsolutePath()
                                                                                                + this.directoryEntries.get(selectionRowID));
                                        break;
                                case ABSOLUTE:
                                        clickedFile = new File(this.directoryEntries.get(selectionRowID));
                                        break;
                        }
                        if(clickedFile.isFile())
                            this.Browse(clickedFile);
                }
        }

        private void readFile() {
// what to write here???
        }

Извините, я не могу вставить изображение, потому что у меня нет репутации, но когда я запускаю его на своем эмуляторе, я получаю что-то вроде этого:

 /mnt/sdcard/kuzmanic.c
/mnt/sdcard/text.txt
/mnt/sdcard/DCIM
/mnt/sdcard/LOST.DIR

Итак то, что я хочу сделать, это когда я нажимаю на файл text.txt или kuzmanic.c, который я хочу открыть, затем в том же файле макета, который является моим файлом load.xml:

This is the code for the xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:textSize="18sp">    


</TextView>

Что мне нужно написать в моем коде и мне нужно что-то писать в манифесте???

5
задан 11 May 2012 в 10:34
поделиться