Issue
I'm having a little trouble, im executing a method in my doinBackground task, so I'm getting a crash because im accesing to another class without finishing this method first, so i want to add a return or something to let the method know when it needs to launch the other activity. I have searched and I can't return a boolean, true or false into Firebase asynctask method. This is the method I use to download a file and replace it into internal memory, but when im doing this , the other activity I need to launch after this launches and i get a crash, so i need to first execute this download task and then if something is true launch my other activity
This is where I want to put a boolean or something that tells me that the download finished.
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("TamañoArchivo",""+taskSnapshot.getTotalByteCount());
Log.e("NombreArchivo",""+xFile);
try {
FileOutputStream fos = context.openFileOutput("pictos.txt", Context.MODE_PRIVATE);
fos.write(getStringFromFile(xFile.getAbsolutePath()).getBytes());
Log.e("xFILEDESCARGARPAIS",""+getStringFromFile(xFile.getAbsolutePath()));
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
The method is not an asyncTask, is an async but from Firebase, this is the method:
public boolean DescargarArchivosPais(String locale){
File rootPath = new File(context.getCacheDir(),"MY_FILES");
if(!rootPath.exists()) {
rootPath.mkdirs();//si no existe el directorio lo creamos
}
StorageReference mStorageRef2 = FirebaseStorage.getInstance().getReference().child("Files/y/" + "y_" + locale + "." + "txt");
StorageReference mStorageRef1 = FirebaseStorage.getInstance().getReference().child("Files/x/" + "x_" + locale + "." + "txt");
Log.e("REFERENCIAx",""+ mStorageRef1);
Log.e("REFERENCIAy",""+ mStorageRef2);
final File xFile = new File(rootPath, "x.txt");
final File yFile = new File(rootPath, "y.txt");
mStorageRef1.getFile(xFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("TamañoArchivo",""+taskSnapshot.getTotalByteCount());
Log.e("NombreArchivo",""+xFile);
try {
FileOutputStream fos = context.openFileOutput("x.txt", Context.MODE_PRIVATE);
fos.write(getStringFromFile(xFile.getAbsolutePath()).getBytes());
Log.e("LOG",""+getStringFromFile(xFile.getAbsolutePath()));
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
mStorageRef2.getFile(yFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("TamañoArchivo",""+taskSnapshot.getTotalByteCount());
Log.e("NombreArchivo",""+yFile);
try {
FileOutputStream fos = context.openFileOutput("y.txt", Context.MODE_PRIVATE);
fos.write(getStringFromFile(gruposFile.getAbsolutePath()).getBytes());
Log.e("LOG2",""+getStringFromFile(gruposFile.getAbsolutePath()));
fos.close();
fSuccess = true;
} catch (Exception e) {
e.printStackTrace();
Log.e("printStackTrace",""+e.toString());
fSuccess = false;
}
fSuccess = true;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
fSuccess=false;
Log.e("printStackTrace",""+e.toString());
}
});
return fSuccess;
}
Solution
onSuccess()
method has an asynchronous behaviour. This means that in order to use the data that you are getting from Firebase Storage, you need to wait for it. So to do that, there is no need to use an AsyncTask
, you can simply create your own custom callback.
To make this happen, please see the last part for my answer from this post. As Mohammed Atif mentioned in his comment, never use the Activity reference directly because it will cause memory leaks. So the way I mentioned above, is the simplest and safest way in which you can achieve this.
Answered By - Alex Mamo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.