我有聊天屏幕,我正在从FirebaeStorage下载附件。
我有各种格式的文件,可以发送doc,pdf,apk等。对于每个文件,我都有相同的TextViews和ImageViews。
在聊天屏幕的recyclerview适配器中,我正在设置本地存储的文件路径,该路径可以通过运行AsyncTask获得,该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");
}
}
异步任务
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();
}
}
更新正如@Atef Hares所建议的,我确实在code。实现了它的工作正常,但如果我有不同的格式怎么办。从asynctask cuz代码获取结果后如何调用特定的if if建议仅调用pdf if语句。
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"
}
最佳答案
知道了,使用接口就可以实现您所需要的!
就是这样:
public interface AsyncTaskCallback {
void onSuccess (String filePath);
}
并在Adapter中,除非您从asyncTask获得了数据,否则不要在视图中设置任何数据,如下所示:
public void onBindViewHolder(final Chat_Adapter.ViewHolder holder, int position) {
//Initialize filetype here
new DownloadFileFromFS(Download_URL, FileName + "."+ filetype, new AsyncTaskCallback() {
@Override
public void onSuccess(String filePath) {
//HERE I NEED THE RESULT FROM ASYNCTASK AND WAIT TILL DOWNLOAD COMPLETES
//THEN SET THE VIEWS WITH RETURN RESULT FROM ASYNCTASK
if (filePath != null) {
File file = new File(filePath);
long sizeFile = file.length() / 1024; //In KB
holder.Doc_FileName.setText(filePath);
holder.Doc_FileSize.setText(String.valueOf(sizeFile));
} else {
Log.d(TAG, "DOWNLOAD FILE PATH IS NULL");
}
}
}).execute();
}