Android 下载数据并在listview中显示
我已经构建了一个应用程序,用户只需单击一个按钮即可下载图像和附件文本文件(在两个单独的文件夹中,一个文件夹用于存储图像,另一个文件夹用于文本文件) 现在我想构建一个列表视图来显示这些数据,每一行都包含图像(ImageView)和图像名称(textView),当用户单击它时,我想显示附件文本文件 下载文件代码:Android 下载数据并在listview中显示,android,listview,Android,Listview,我已经构建了一个应用程序,用户只需单击一个按钮即可下载图像和附件文本文件(在两个单独的文件夹中,一个文件夹用于存储图像,另一个文件夹用于文本文件) 现在我想构建一个列表视图来显示这些数据,每一行都包含图像(ImageView)和图像名称(textView),当用户单击它时,我想显示附件文本文件 下载文件代码: @Override protected Void doInBackground(String... strings) {
@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;
}
文件下载程序代码:
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);
}
和问题/问题是什么?我希望检索存储的数据并在listview中显示它,因此当单击图像时,我希望显示附件文本文件,直到没有问题为止。仍然没有问题提到。先生,我有2个文件夹目录,一个用于图像,第二个用于txt文件,在这个意义上,每个图像对应的文件txt Q-How检索存储在这个文件夹中的数据,并显示在listview中!!!因此,发布所有下载代码是无关紧要的。用于在ListView中显示文件夹中的图像的代码已多次发布在该网站和其他网站上。谷歌搜索一下会给你很多点击率。