error: incompatible types: Task cannot be converted to Uri [duplicate]












1
















This question already has an answer here:




  • How to use getdownloadurl in recent versions?

    3 answers




I want to fetch Url of the Image that I have uploaded in the Firebase. To fetch the Image I need to have the Url of the specific Image. But there's is problem occurring in the fetching. The Output box shows the following:




error: incompatible types: Task cannot be converted to Uri




If I use Task<Uri> the image url become different. As compare to Uri.



  private void startPosting() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();

final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri down=ref.getDownloadUrl();
Uri dow=down;
newPost.child("ImageUrl").setValue(ref.getDownloadUrl());
// newPost.child("Positivity").setValue(PositiveDesc);
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");

}
});

}
}


The error is flashing on this line of code:



Uri down=ref.getDownloadUrl();









share|improve this question















marked as duplicate by Frank van Puffelen android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    getDownloadUrl returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see stackoverflow.com/questions/51056397/… and the longer: firebase.google.com/docs/storage/android/…

    – Frank van Puffelen
    Nov 15 '18 at 14:06
















1
















This question already has an answer here:




  • How to use getdownloadurl in recent versions?

    3 answers




I want to fetch Url of the Image that I have uploaded in the Firebase. To fetch the Image I need to have the Url of the specific Image. But there's is problem occurring in the fetching. The Output box shows the following:




error: incompatible types: Task cannot be converted to Uri




If I use Task<Uri> the image url become different. As compare to Uri.



  private void startPosting() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();

final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri down=ref.getDownloadUrl();
Uri dow=down;
newPost.child("ImageUrl").setValue(ref.getDownloadUrl());
// newPost.child("Positivity").setValue(PositiveDesc);
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");

}
});

}
}


The error is flashing on this line of code:



Uri down=ref.getDownloadUrl();









share|improve this question















marked as duplicate by Frank van Puffelen android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    getDownloadUrl returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see stackoverflow.com/questions/51056397/… and the longer: firebase.google.com/docs/storage/android/…

    – Frank van Puffelen
    Nov 15 '18 at 14:06














1












1








1









This question already has an answer here:




  • How to use getdownloadurl in recent versions?

    3 answers




I want to fetch Url of the Image that I have uploaded in the Firebase. To fetch the Image I need to have the Url of the specific Image. But there's is problem occurring in the fetching. The Output box shows the following:




error: incompatible types: Task cannot be converted to Uri




If I use Task<Uri> the image url become different. As compare to Uri.



  private void startPosting() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();

final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri down=ref.getDownloadUrl();
Uri dow=down;
newPost.child("ImageUrl").setValue(ref.getDownloadUrl());
// newPost.child("Positivity").setValue(PositiveDesc);
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");

}
});

}
}


The error is flashing on this line of code:



Uri down=ref.getDownloadUrl();









share|improve this question

















This question already has an answer here:




  • How to use getdownloadurl in recent versions?

    3 answers




I want to fetch Url of the Image that I have uploaded in the Firebase. To fetch the Image I need to have the Url of the specific Image. But there's is problem occurring in the fetching. The Output box shows the following:




error: incompatible types: Task cannot be converted to Uri




If I use Task<Uri> the image url become different. As compare to Uri.



  private void startPosting() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();

final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri down=ref.getDownloadUrl();
Uri dow=down;
newPost.child("ImageUrl").setValue(ref.getDownloadUrl());
// newPost.child("Positivity").setValue(PositiveDesc);
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");

}
});

}
}


The error is flashing on this line of code:



Uri down=ref.getDownloadUrl();




This question already has an answer here:




  • How to use getdownloadurl in recent versions?

    3 answers








android firebase firebase-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:06









Frank van Puffelen

239k29383410




239k29383410










asked Nov 15 '18 at 12:40









itz Prateekitz Prateek

349




349




marked as duplicate by Frank van Puffelen android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Frank van Puffelen android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    getDownloadUrl returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see stackoverflow.com/questions/51056397/… and the longer: firebase.google.com/docs/storage/android/…

    – Frank van Puffelen
    Nov 15 '18 at 14:06














  • 1





    getDownloadUrl returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see stackoverflow.com/questions/51056397/… and the longer: firebase.google.com/docs/storage/android/…

    – Frank van Puffelen
    Nov 15 '18 at 14:06








1




1





getDownloadUrl returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see stackoverflow.com/questions/51056397/… and the longer: firebase.google.com/docs/storage/android/…

– Frank van Puffelen
Nov 15 '18 at 14:06





getDownloadUrl returns a task, which then gives you the download URL once it completes. For a code sample of how to do this, see stackoverflow.com/questions/51056397/… and the longer: firebase.google.com/docs/storage/android/…

– Frank van Puffelen
Nov 15 '18 at 14:06












2 Answers
2






active

oldest

votes


















1














You are getting the following error:




incompatible types: Task cannot be converted to Uri




Because ref.getDownloadUrl() return a Task object and not a Uri object. There is no way in Java to cast a Task object to a Uri object.



In order to get the download url, you need to use addOnSuccessListener, like in the following lines of code:



ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();

//Do what you need to do with url
}
});
}
});





share|improve this answer
























  • Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

    – itz Prateek
    Nov 15 '18 at 13:31






  • 1





    Yes but it should work. Please edit your question to see your changed code.

    – Alex Mamo
    Nov 15 '18 at 13:57













  • Yes you are right.. Thank you so much @AlexMamo

    – itz Prateek
    Nov 15 '18 at 19:45



















1














Make sure if you are using a task to check for it if isSuccessful()



To get the download URL of the file you are uploading, you should run this inside a task like this one.



First, we do a reference where to store your file, then we run a task waiting for the result, if the result is true, in this case, we return the downloadUrl of the file, then when we .addOnCompleteListener , that's where we know we already have uploaded the file and have the URL, then we just check again if the upload process has been done correctly and we fetch the download URL of that file with



Uri downloadUrl = task.getResult(); , since it's a URI, we can convert this url into an string like this String downloadUrl = task.getResult().toString(); or simply downloadUrl.toString();



final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri downloadUrl = task.getResult();

newPost.child("ImageUrl").setValue(downloadUrl.toString());
// newPost.child("Positivity").setValue(PositiveDesc);

} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});


The only thing is that .addOnProgressListener won't work here.






share|improve this answer


























  • Thank you so much for the help..

    – itz Prateek
    Nov 15 '18 at 14:54


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You are getting the following error:




incompatible types: Task cannot be converted to Uri




Because ref.getDownloadUrl() return a Task object and not a Uri object. There is no way in Java to cast a Task object to a Uri object.



In order to get the download url, you need to use addOnSuccessListener, like in the following lines of code:



ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();

//Do what you need to do with url
}
});
}
});





share|improve this answer
























  • Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

    – itz Prateek
    Nov 15 '18 at 13:31






  • 1





    Yes but it should work. Please edit your question to see your changed code.

    – Alex Mamo
    Nov 15 '18 at 13:57













  • Yes you are right.. Thank you so much @AlexMamo

    – itz Prateek
    Nov 15 '18 at 19:45
















1














You are getting the following error:




incompatible types: Task cannot be converted to Uri




Because ref.getDownloadUrl() return a Task object and not a Uri object. There is no way in Java to cast a Task object to a Uri object.



In order to get the download url, you need to use addOnSuccessListener, like in the following lines of code:



ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();

//Do what you need to do with url
}
});
}
});





share|improve this answer
























  • Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

    – itz Prateek
    Nov 15 '18 at 13:31






  • 1





    Yes but it should work. Please edit your question to see your changed code.

    – Alex Mamo
    Nov 15 '18 at 13:57













  • Yes you are right.. Thank you so much @AlexMamo

    – itz Prateek
    Nov 15 '18 at 19:45














1












1








1







You are getting the following error:




incompatible types: Task cannot be converted to Uri




Because ref.getDownloadUrl() return a Task object and not a Uri object. There is no way in Java to cast a Task object to a Uri object.



In order to get the download url, you need to use addOnSuccessListener, like in the following lines of code:



ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();

//Do what you need to do with url
}
});
}
});





share|improve this answer













You are getting the following error:




incompatible types: Task cannot be converted to Uri




Because ref.getDownloadUrl() return a Task object and not a Uri object. There is no way in Java to cast a Task object to a Uri object.



In order to get the download url, you need to use addOnSuccessListener, like in the following lines of code:



ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();

//Do what you need to do with url
}
});
}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 12:49









Alex MamoAlex Mamo

45k82863




45k82863













  • Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

    – itz Prateek
    Nov 15 '18 at 13:31






  • 1





    Yes but it should work. Please edit your question to see your changed code.

    – Alex Mamo
    Nov 15 '18 at 13:57













  • Yes you are right.. Thank you so much @AlexMamo

    – itz Prateek
    Nov 15 '18 at 19:45



















  • Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

    – itz Prateek
    Nov 15 '18 at 13:31






  • 1





    Yes but it should work. Please edit your question to see your changed code.

    – Alex Mamo
    Nov 15 '18 at 13:57













  • Yes you are right.. Thank you so much @AlexMamo

    – itz Prateek
    Nov 15 '18 at 19:45

















Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

– itz Prateek
Nov 15 '18 at 13:31





Method does not override method from its superclass. you are using onSuccess in a nested way. @AlexMamo

– itz Prateek
Nov 15 '18 at 13:31




1




1





Yes but it should work. Please edit your question to see your changed code.

– Alex Mamo
Nov 15 '18 at 13:57







Yes but it should work. Please edit your question to see your changed code.

– Alex Mamo
Nov 15 '18 at 13:57















Yes you are right.. Thank you so much @AlexMamo

– itz Prateek
Nov 15 '18 at 19:45





Yes you are right.. Thank you so much @AlexMamo

– itz Prateek
Nov 15 '18 at 19:45













1














Make sure if you are using a task to check for it if isSuccessful()



To get the download URL of the file you are uploading, you should run this inside a task like this one.



First, we do a reference where to store your file, then we run a task waiting for the result, if the result is true, in this case, we return the downloadUrl of the file, then when we .addOnCompleteListener , that's where we know we already have uploaded the file and have the URL, then we just check again if the upload process has been done correctly and we fetch the download URL of that file with



Uri downloadUrl = task.getResult(); , since it's a URI, we can convert this url into an string like this String downloadUrl = task.getResult().toString(); or simply downloadUrl.toString();



final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri downloadUrl = task.getResult();

newPost.child("ImageUrl").setValue(downloadUrl.toString());
// newPost.child("Positivity").setValue(PositiveDesc);

} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});


The only thing is that .addOnProgressListener won't work here.






share|improve this answer


























  • Thank you so much for the help..

    – itz Prateek
    Nov 15 '18 at 14:54
















1














Make sure if you are using a task to check for it if isSuccessful()



To get the download URL of the file you are uploading, you should run this inside a task like this one.



First, we do a reference where to store your file, then we run a task waiting for the result, if the result is true, in this case, we return the downloadUrl of the file, then when we .addOnCompleteListener , that's where we know we already have uploaded the file and have the URL, then we just check again if the upload process has been done correctly and we fetch the download URL of that file with



Uri downloadUrl = task.getResult(); , since it's a URI, we can convert this url into an string like this String downloadUrl = task.getResult().toString(); or simply downloadUrl.toString();



final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri downloadUrl = task.getResult();

newPost.child("ImageUrl").setValue(downloadUrl.toString());
// newPost.child("Positivity").setValue(PositiveDesc);

} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});


The only thing is that .addOnProgressListener won't work here.






share|improve this answer


























  • Thank you so much for the help..

    – itz Prateek
    Nov 15 '18 at 14:54














1












1








1







Make sure if you are using a task to check for it if isSuccessful()



To get the download URL of the file you are uploading, you should run this inside a task like this one.



First, we do a reference where to store your file, then we run a task waiting for the result, if the result is true, in this case, we return the downloadUrl of the file, then when we .addOnCompleteListener , that's where we know we already have uploaded the file and have the URL, then we just check again if the upload process has been done correctly and we fetch the download URL of that file with



Uri downloadUrl = task.getResult(); , since it's a URI, we can convert this url into an string like this String downloadUrl = task.getResult().toString(); or simply downloadUrl.toString();



final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri downloadUrl = task.getResult();

newPost.child("ImageUrl").setValue(downloadUrl.toString());
// newPost.child("Positivity").setValue(PositiveDesc);

} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});


The only thing is that .addOnProgressListener won't work here.






share|improve this answer















Make sure if you are using a task to check for it if isSuccessful()



To get the download URL of the file you are uploading, you should run this inside a task like this one.



First, we do a reference where to store your file, then we run a task waiting for the result, if the result is true, in this case, we return the downloadUrl of the file, then when we .addOnCompleteListener , that's where we know we already have uploaded the file and have the URL, then we just check again if the upload process has been done correctly and we fetch the download URL of that file with



Uri downloadUrl = task.getResult(); , since it's a URI, we can convert this url into an string like this String downloadUrl = task.getResult().toString(); or simply downloadUrl.toString();



final StorageReference ref = mStorage.child("Quote_Image" + UUID.randomUUID().toString());
ref.putFile(filePath).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
Toast.makeText(adminUpdateQuotesSend.this , "Uploaded" , Toast.LENGTH_SHORT).show();
mDatbase = FirebaseDatabase.getInstance().getReference().child("Quotes");
DatabaseReference newPost = mDatbase.push();
Uri downloadUrl = task.getResult();

newPost.child("ImageUrl").setValue(downloadUrl.toString());
// newPost.child("Positivity").setValue(PositiveDesc);

} else {
Toast.makeText(mContext, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});


The only thing is that .addOnProgressListener won't work here.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 14 at 20:21









halfer

14.6k758114




14.6k758114










answered Nov 15 '18 at 12:52









Gastón SaillénGastón Saillén

3,77841234




3,77841234













  • Thank you so much for the help..

    – itz Prateek
    Nov 15 '18 at 14:54



















  • Thank you so much for the help..

    – itz Prateek
    Nov 15 '18 at 14:54

















Thank you so much for the help..

– itz Prateek
Nov 15 '18 at 14:54





Thank you so much for the help..

– itz Prateek
Nov 15 '18 at 14:54



Popular posts from this blog

Bressuire

Vorschmack

Quarantine