我有聊天屏幕,我正在从FirebaeStorage下载附件。 我有各种格式的文件,可以发送doc,pdf,apk等,每个我有相同的TextViews和ImageViews。将onPostExecute值返回到RecyclerAdapter的onBindViewHolder中
在聊天屏幕的recyclerview适配器我设置本地存储的文件路径,它可以通过运行AsyncTask从Firebase存储中下载文件并返回文件路径来获取。此工作完美,但问题是如何找回文件路径onBindViewHolder上特别是如果其他
这里是我的RecyclerAdapter这里我打电话的AsyncTask和需要结果返回到相同的范围和等到下载完成,然后根据数据集视图返回
public void onBindViewHolder(final Chat_Adapter.ViewHolder holder, int position) {
if (fileType.equals("pdf")){
new DownloadFileFromFS(Download_URL,FileName+".pdf").execute();
//HERE I NEED THE RESULT FROM ASYNCTASK AND WAIT TILL DOWNLOAD COMPLETES
//THEN SET THE VIEWS WITH RETURN RESULT FROM ASYNCTASK
if (DownloadFilePath!=null){
File file=new File(DownloadFilePath);
long sizeFile=file.length()/1024; //In KB
holder.Doc_FileName.setText(DownloadFilePath);
holder.Doc_FileSize.setText(String.valueOf(sizeFile));
}
else {
Log.d(TAG,"DOWNLOAD FILE PATH IS NULL");
}
}
的AsyncTask
public class DownloadAttachment extends AsyncTask<Void, String, String> {
String DownloadUrl,fileName;
File file;
Context context;
ProgressBar progressBar;
public static final String TAG="###Download Attachment";
public DownloadAttachment(Context context, String downloadUrl, String fileName) {
DownloadUrl = downloadUrl;
this.fileName = fileName;
this.context=context;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected String doInBackground(Void... params) {
int count;
try {
File root = android.os.Environment.getExternalStorageDirectory();
Log.d(TAG,"DO IN BACKGROUND RUNNING");
File dir = new File(root.getAbsolutePath() + "/Downloaded Files/");
if (dir.exists() == false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d(TAG, "download begining");
Log.d(TAG, "download url:" + url);
Log.d(TAG, "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
//this will be useful so that you can show a typical 0-100% progress bar
int lengthOfFile=ucon.getContentLength();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayOutputStream baf = new ByteArrayOutputStream(5000);
int current = 0;
long total=0;
while ((current = bis.read()) != -1) {
baf.write((byte) current);
total=total+current;
//PUBLISH THE PROGRESS
//AFTER THIS onProgressUpdate will be called
publishProgress(""+(int)(total*100)/lengthOfFile);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager", "download ready in " + ((System.currentTimeMillis() - startTime)/1000) + " sec");
Log.d(TAG,"File Path "+file);
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
return file.toString();
}
}
UPDATE作为@Atef野兔建议,我没有在code.its实施工作正常,但如果我有不同的格式。如果从Asynctask获得结果后如何调用特定的cuz代码,建议只在调用pdf时声明。
if (fileType.equals("pdf")){
final String nameFile=UUID.randomUUID().toString();
new DownloadFileFromFS(chat_wrapper.getDocuments(),nameFile).execute();
filePathInterface=new FilePath() {
@Override
public void LocalFilePath(final String Path) {
//HERE I AM GETTING RESULT FROM ASYNCTASK
//AND SETTING VIEWS ACCORDING TO IT
}
};
}
else if (fileType.equals("doc")){
//HOW TO GET RESULT FROM ASYNCTASK HERE IF FILETYPE IS "doc"
}
else if (fileType.equals("ppt")){
//HOW TO GET RESULT FROM ASYNCTASK HERE IF FILETYPE IS "ppt"
}
见我的答案[这里](http://stackoverflow.com/a/42656227/5993410) –
@AtefHares我使用界面来获得结果回到活动,但问题来了,我不得不等待下载完成,然后设置与数据的意见,并没有等待它返回null – Ritu
为什么你从调用调用Asynctask [R?你不应该使用适配器,除非你有数据将被显示,而且,不要从适配器调用asynctask,从活动/片段调用它并获取所需的数据,然后将其传递给适配器以显示它 –