我正在尝试运行一个名为 uploadProfilePhoto()的方法,但是即使uploadProfilePhoto()尚未完成运行,该方法调用后的代码也会先运行。无论如何,我可以先让uploadProfilePhoto()先完成它的过程,然后再继续吗?
我尝试使用AsynTask,但仍然无法正常工作。似乎是因为 doInBackground()用于一系列代码,而不是方法。
AsyncTask
protected class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
uploadProfilePhoto();
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(ProfileSetup.this, "Account created successfully.", Toast.LENGTH_SHORT).show();
mAuth.signInWithEmailAndPassword(email, password);
startActivity(new Intent(ProfileSetup.this, MainActivity.class));
}
}
uploadProfilePhoto()
private void uploadProfilePhoto() {
if (mImageUri != null) {
final StorageReference imageReference = storageReference.child(System.currentTimeMillis() + "." + getFileExtension(mImageUri));
storageTask = imageReference.putFile(mImageUri);
storageTask.continueWithTask(new Continuation() {
@Override
public Task<Uri> then(@NonNull Task task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return imageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
myUrl = downloadUri.toString();
String myid = getIntent().getStringExtra("uid");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(myid);
reference.child("profilePicUrl").setValue(myUrl);
finish();
} else {
Toast.makeText(ProfileSetup.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ProfileSetup.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
我可以想到两种方式
storageTask = imageReference.putFile(mImageUri);
storageTask.addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
myUrl = task.getResult().toString();
String myid = getIntent().getStringExtra("uid");
DatabaseReference reference =
FirebaseDatabase.getInstance().getReference("Users").child(myid);
reference.child("profilePicUrl").setValue(myUrl);
startActivity(new Intent(ProfileSetup.this, MainActivity.class));
finish();
} else {
Toast.makeText(ProfileSetup.this, "Failed",
Toast.LENGTH_SHORT).show();
}
}
实际上不是很干净也不太好的第二种方法应该被认为是不好的做法,但是知道替代方法没有什么害处
您说完imageReference.putFlie(mImageUri);
imgeReference.putFile(mImageUri);
startActivity(new Intent(ProfileSetup.this, MainActivity.class));
现在进入MainActivity
while(ProfileSetup.getImageUrl() == null){ // define a method to return the url
Systemclock.sleep(500); // if it is small image you can hote it will be uplaoded in seconds
}
Picasso.with(this).load(ProfileSetup.getImageUrl()).fit().into(imageView); // the while loop terminates when your image gets uploaded because getImageUrl() did not return null , now you know image is there you can download and set it to imageView by using Piccaso
使用这种方法,您不需要completeListener,但我不建议这样做,因为它更复杂并且不会处理imageUploading的任何失败