下载数据并在列表视图中显示

时间:2015-03-27 16:42:07

标签: android listview

我已经构建了一个应用程序,用户可以通过一个按钮单击图像和附件文本文件(在两个单独的文件夹中,一个文件夹用于存储图像,另一个用于文本文件)

现在我想构建一个listview来显示这个数据,每行包含图像(ImageView)和图像名称(textView),当用户点击它时我想显示附件文本文件

下载文件代码:

            @Override
            protected Void doInBackground(String... strings) {
                String filetxtUrl = strings[0];   
                String filetxtName = strings[1];  
                String fileImageUrl = strings[2];
                String fileImageName = strings[3];


                String extStorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); //(Environment.DIRECTORY_DOWNLOADS) 
                File folder = new File(extStorageDirectory, "folder");
                if (!folder.exists()) {
                    folder.mkdirs();
                }


                File folder1 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"
                        + "folder" , "txt");
                if (!folder1.exists()) {
                    folder1.mkdirs();
                }

                File TxtFile = new File(folder1, filetxtName);


                File folder3 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"
                        + "folder" , "Image");
                if (!folder3.exists()) {
                    folder3.mkdirs();
                }

                File ImageFile = new File(folder3, fileImageName);

                try{
                    TxtFile.createNewFile();
                    ImageFile.createNewFile();
                }catch (IOException e){
                    e.printStackTrace();
                }
                FileDownloader.downloadFile(filetxtUrl, TxtFile);
                FileDownloader.downloadFile(fileImageUrl, ImageFile);

                L.m("File Download successufuly in Sdcard0 ");    

                return null;
            }

FileDownloader代码:

        private static final int  MEGABYTE = 1024 * 1024;

        public static void downloadFile(String fileUrl, File directory){
            try {

                URL url = new URL(fileUrl);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();


                urlConnection.connect();

                InputStream inputStream = urlConnection.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(directory);


                byte[] buffer = new byte[MEGABYTE];
                int bufferLength = 0;
                while((bufferLength = inputStream.read(buffer))>0 ){
                    fileOutputStream.write(buffer, 0, bufferLength);
                }
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
             L.m(""+e);   
             } catch (MalformedURLException e) {
                 L.m(""+e);   
            } catch (IOException e) {
                 L.m(""+e);   
            }
        }   
}

我想使用下面的代码来阅读txt文件附件

public void displayOutput()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/TextFile.txt");
    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) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } 
    catch (FileNotFoundException e) {
        Toast.makeText(getApplicationContext(),"File not found!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    TextView output=(TextView) findViewById(R.id.output); 
    // Assuming that 'output' is the id of your TextView
    output.setText(text);
} 

0 个答案:

没有答案