无法在 java 中获取图像 url
Cannot get the image url in java
我需要在将图像上传到 firebase 存储后获取图像 URL。之后,图像 URL 将与其他用户数据一起添加到我的 firebase 数据库中。然而,link 一直被认为是 abc,我想知道为什么它不会被
覆盖
link = task.getResult().getStorage().getDownloadUrl().toString();
在uploadImage()
方法中。有人能帮我吗?
提前致谢:)
public class A extends AppCompatActivity {
private Button btnChoose, btnUpload;
private ImageView imageView;
private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;
private DatabaseReference mDatabase;
private String username;
private FirebaseStorage storage;
private StorageReference storageReference;
private String role = "therapist";
private FirebaseAuth mAuth;
private String email, psd, link = "abc";
private UploadTask upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("name");
email = extras.getString("email");
psd = extras.getString("psd");
}
btnChoose = (Button) findViewById(R.id.choosedevice);
btnUpload = (Button) findViewById(R.id.nexttothome);
imageView = (ImageView) findViewById(R.id.viewImage);
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
Signup();
}
});
compname = findViewById(R.id.compnameregister);
adr1 = findViewById(R.id.adr1register);
adr2 = findViewById(R.id.adr2register);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
mDatabase = FirebaseDatabase.getInstance().getReference();
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
private void uploadImage() {
if (filePath != null) {
final StorageReference ref = storageReference.child("images/" + UUID.randomUUID().toString());
ref.putFile(filePath).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo2.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
link = task.getResult().getStorage().getDownloadUrl().toString();
}
}
});
}
}
private void Signup() {
mAuth.createUserWithEmailAndPassword(email, psd).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
//check if successful
if (task.isSuccessful()) {
onAuthSuccess(task.getResult().getUser());
} else {
Toast.makeText(A.this, "Couldn't register, try again", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void onAuthSuccess(FirebaseUser user) {
String compname1 = compname.getText().toString().trim();
String adr11 = adr1.getText().toString().trim();
String adr21 = adr2.getText().toString().trim();
// Write new user
sendToDatabase(user.getUid(), username, role, link, compname1, adr11, adr21);
}
private void sendToDatabase(String userId, String username, String role, String link, String compname1, String adr11, String adr21) {
User thera = new User(username, role, link);
Company comp = new Company(compname1, adr11, adr21);
mDatabase.child("user").child(userId).setValue(thera);
mDatabase.child("company").child(userId).setValue(comp);
}
}
您的 Signup() 方法似乎在 uploadImage() 中的异步任务 运行 之前触发。尝试将 Signup() 放入 onComplete() in uploadImage().
比如,
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo2.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
link=task.getResult().getStorage().getDownloadUrl().toString();
Signup();
}
}
我需要在将图像上传到 firebase 存储后获取图像 URL。之后,图像 URL 将与其他用户数据一起添加到我的 firebase 数据库中。然而,link 一直被认为是 abc,我想知道为什么它不会被
覆盖link = task.getResult().getStorage().getDownloadUrl().toString();
在uploadImage()
方法中。有人能帮我吗?
提前致谢:)
public class A extends AppCompatActivity {
private Button btnChoose, btnUpload;
private ImageView imageView;
private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;
private DatabaseReference mDatabase;
private String username;
private FirebaseStorage storage;
private StorageReference storageReference;
private String role = "therapist";
private FirebaseAuth mAuth;
private String email, psd, link = "abc";
private UploadTask upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("name");
email = extras.getString("email");
psd = extras.getString("psd");
}
btnChoose = (Button) findViewById(R.id.choosedevice);
btnUpload = (Button) findViewById(R.id.nexttothome);
imageView = (ImageView) findViewById(R.id.viewImage);
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
Signup();
}
});
compname = findViewById(R.id.compnameregister);
adr1 = findViewById(R.id.adr1register);
adr2 = findViewById(R.id.adr2register);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
mDatabase = FirebaseDatabase.getInstance().getReference();
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
private void uploadImage() {
if (filePath != null) {
final StorageReference ref = storageReference.child("images/" + UUID.randomUUID().toString());
ref.putFile(filePath).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo2.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
link = task.getResult().getStorage().getDownloadUrl().toString();
}
}
});
}
}
private void Signup() {
mAuth.createUserWithEmailAndPassword(email, psd).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
//check if successful
if (task.isSuccessful()) {
onAuthSuccess(task.getResult().getUser());
} else {
Toast.makeText(A.this, "Couldn't register, try again", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void onAuthSuccess(FirebaseUser user) {
String compname1 = compname.getText().toString().trim();
String adr11 = adr1.getText().toString().trim();
String adr21 = adr2.getText().toString().trim();
// Write new user
sendToDatabase(user.getUid(), username, role, link, compname1, adr11, adr21);
}
private void sendToDatabase(String userId, String username, String role, String link, String compname1, String adr11, String adr21) {
User thera = new User(username, role, link);
Company comp = new Company(compname1, adr11, adr21);
mDatabase.child("user").child(userId).setValue(thera);
mDatabase.child("company").child(userId).setValue(comp);
}
}
您的 Signup() 方法似乎在 uploadImage() 中的异步任务 运行 之前触发。尝试将 Signup() 放入 onComplete() in uploadImage().
比如,
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo2.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();
link=task.getResult().getStorage().getDownloadUrl().toString();
Signup();
}
}