首先,我使用裁剪活动从用户那里获取图像,然后将其压缩并创建一个拇指图像。我将它们上传到存储设备中,并将其下载网址保存在数据库中。由于某些原因,图像已上载并检索到链接,但thumb_image已上载但未检索到链接。请帮帮我。我在做什么错了?
mprogbar.setVisibility(View.VISIBLE);
mylay.setClickable(false);
Uri resultUri= result.getUri();
final File thumb_filePath = new File(resultUri.getPath());
Bitmap thumb_bitmap = new Compressor(SettingsActivity.this).
setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
final StorageReference filepath = mstore.child("profile_pictures").child(mcurrentuser+".jpg");
final StorageReference thumbfilepath = mstore.child("profile_pictures").child("thumbs").child(mcurrentuser+".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful())
{
final String[] downloadurl = {""};
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
downloadurl[0] =uri.toString();
}
});
thumbfilepath.putBytes(thumb_byte).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {
if(thumb_task.isSuccessful())
{
final String[] thumb_downloadurl = {""};
thumbfilepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
thumb_downloadurl[0] =uri.toString();
}
});
Map mymap= new HashMap();
mymap.put("image", downloadurl[0]);
mymap.put("thumb_image", thumb_downloadurl[0]);
mdatabase.updateChildren(mymap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
mprogbar.setVisibility(View.INVISIBLE);
mylay.setClickable(true);
Toast.makeText(SettingsActivity.this,"Profile Picture updated",
Toast.LENGTH_LONG).show();
}
}
});
}
else
{
mprogbar.setVisibility(View.INVISIBLE);
mylay.setClickable(true);
Toast.makeText(SettingsActivity.this,"Profile Picture could not be updated",
Toast.LENGTH_LONG).show();
}
}
});
}
else
{
mprogbar.setVisibility(View.INVISIBLE);
}
thumb_downloadurl [0]只是一个空字符串
答案 0 :(得分:0)
获取下载URL是异步操作。当您调用getDownloadUrl()
时,Firebase SDK 启动,从服务器获取下载URL。检索下载URL时,它将调用您的onSuccess
方法。
在等待的过程中,Firebase允许您继续执行onSuccess
回调之外的代码,因为Android否则将显示“应用程序无响应”对话框。这是您的带有mymap.put("image", downloadurl[0])
的代码运行的时候,此时还没有从服务器返回下载URL。
需要访问下载URL的任何代码都必须在onSuccess
方法的内部中调用。像这样:
thumbfilepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
thumb_downloadurl[0] =uri.toString();
Map mymap= new HashMap();
mymap.put("image", downloadurl[0]);
mymap.put("thumb_image", thumb_downloadurl[0]);
mdatabase.updateChildren(mymap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
mprogbar.setVisibility(View.INVISIBLE);
mylay.setClickable(true);
Toast.makeText(SettingsActivity.this,"Profile Picture updated",
Toast.LENGTH_LONG).show();
}
}
});
}
});
在这里使用Task
API可以做很多事情。我建议阅读道格的blog posts on becoming a task master系列。