Android 无法获取上载文件的URL
我试图获取上传文件的URL,但我得到了:com.google.android.gms.tasks。zzu@a12a0cb或者类似的东西 以下是我尝试过的代码kotlin: val uid=UUID.randomuid.toString val storageRef=FirebaseStorage.getInstance.reference.childcontent/$uid/$uid.jpg storageRef.putFilefile.addOnSuccessListener{taskSnapShot-> val downloadUrl=storageRef.downloadUrl FirebaseDatabase.getInstance.reference.childPhotos.childdate.push.setValuedownloadUrlAndroid 无法获取上载文件的URL,android,firebase,firebase-realtime-database,firebase-storage,Android,Firebase,Firebase Realtime Database,Firebase Storage,我试图获取上传文件的URL,但我得到了:com.google.android.gms.tasks。zzu@a12a0cb或者类似的东西 以下是我尝试过的代码kotlin: val uid=UUID.randomuid.toString val storageRef=FirebaseStorage.getInstance.reference.childcontent/$uid/$uid.jpg storageRef.putFilefile.addOnSuccessListener{taskSna
} 您可能正在使用旧版本的Firebase存储。您想要获取URL的方式已被弃用,请查看更改日志 我没有Kotlin代码,但是java代码是
final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.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();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
您可以在的Android文档中的“Android>上载文件>获取下载URL”部分找到此代码。您可能正在使用旧版本的Firebase storage。您想要获取URL的方式已被弃用,请查看更改日志 我没有Kotlin代码,但是java代码是
final StorageReference ref = storageRef.child("images/mountains.jpg");
uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.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();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
您可以在的Android文档中的“Android>上载文件>获取下载URL”一节中找到此代码。此代码适用于我
//Data is the uri of the file
UploadTask uploadTask =mProfileStorageRef.child("profilePic").putFile(data);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
task.getResult().getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (task.isSuccessful()) {
//Url is here
String url = uri.toString();
}
});
这个代码对我有用
//Data is the uri of the file
UploadTask uploadTask =mProfileStorageRef.child("profilePic").putFile(data);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
task.getResult().getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (task.isSuccessful()) {
//Url is here
String url = uri.toString();
}
});
我也有同样的问题。我刚刚解决了。 我不能确切地说为什么它不能与其他语法一起工作,但我是这样做的:Firebase实现版本:16.0.1/Kotlin
mReference.putFile (uri) .addOnFailureListener {
// failure
} .addOnSuccessListener () {taskSnapshot ->
// success
mReference.downloadUrl.addOnCompleteListener () {taskSnapshot ->
var url = taskSnapshot.result
println ("url =" + url.toString ())
}
}
我也有同样的问题。我刚刚解决了。 我不能确切地说为什么它不能与其他语法一起工作,但我是这样做的:Firebase实现版本:16.0.1/Kotlin
mReference.putFile (uri) .addOnFailureListener {
// failure
} .addOnSuccessListener () {taskSnapshot ->
// success
mReference.downloadUrl.addOnCompleteListener () {taskSnapshot ->
var url = taskSnapshot.result
println ("url =" + url.toString ())
}
}
这是我的项目代码的一部分,它与您的问题相同,但在进行必要的修改后,现在运行良好,并进行相应的更改
final StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (task.isSuccessful()){
String download_url=uri.toString();//Here is the URL
mUserDatabase.child("image").setValue(download_url)/*Storing the URL in the Firebase database*/
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
/*mProgressDialog.dismiss();
Toast.makeText(SettingsActivity.this, "Success Uploading", Toast.LENGTH_LONG).show();*/
}
}
});
}else {
/*Toast.makeText(SettingsActivity.this, "Error in uploading!", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();*/
}
}
});
}
});
这是我的项目代码的一部分,它与您的问题相同,但在进行必要的修改后,现在运行良好,并进行相应的更改
final StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (task.isSuccessful()){
String download_url=uri.toString();//Here is the URL
mUserDatabase.child("image").setValue(download_url)/*Storing the URL in the Firebase database*/
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
/*mProgressDialog.dismiss();
Toast.makeText(SettingsActivity.this, "Success Uploading", Toast.LENGTH_LONG).show();*/
}
}
});
}else {
/*Toast.makeText(SettingsActivity.this, "Error in uploading!", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();*/
}
}
});
}
});
如果您正在与kotlin一起工作,那么此代码基于官方文档,工作起来很有魅力
fun fileUpload() {
mFireBaseStorage = FirebaseStorage.getInstance()
mphotoStorageReference = mFireBaseStorage.getReference().child("alvaras")
//in case you want to compress your bitmap before upload
val bmp: Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath) //filepath is the URI from the onActivityResult
val baos: ByteArrayOutputStream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos) //25 is the compression, cane be anything between 1 and 100, but 100 is no compression
//get the uri from the bitmap
val tempUri: Uri = getImageUri(this, bmp)
//transform the new compressed bmp in filepath uri
filePath = tempUri //update the filePath variable
var uploadTask = mphotoStorageReference.putFile(filePath)
val urlTask = uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return@Continuation mphotoStorageReference.downloadUrl
}).addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
urifinal = downloadUri.toString() //this is the url you want
val imageView: ImageView = findViewById(R.id.imageView1)
//show it in a imageview with glide with the url
Glide.with(this@MapsActivity).load(urifinal).into(imageView)
imageView.visibility = View.VISIBLE
} else {
// Handle failures
Toast.makeText(this, "An error occur", Toast.LENGTH_SHORT).show()
// ...
}
}
}
如果您正在与kotlin一起工作,那么此代码基于官方文档,工作起来很有魅力
fun fileUpload() {
mFireBaseStorage = FirebaseStorage.getInstance()
mphotoStorageReference = mFireBaseStorage.getReference().child("alvaras")
//in case you want to compress your bitmap before upload
val bmp: Bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath) //filepath is the URI from the onActivityResult
val baos: ByteArrayOutputStream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos) //25 is the compression, cane be anything between 1 and 100, but 100 is no compression
//get the uri from the bitmap
val tempUri: Uri = getImageUri(this, bmp)
//transform the new compressed bmp in filepath uri
filePath = tempUri //update the filePath variable
var uploadTask = mphotoStorageReference.putFile(filePath)
val urlTask = uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return@Continuation mphotoStorageReference.downloadUrl
}).addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
urifinal = downloadUri.toString() //this is the url you want
val imageView: ImageView = findViewById(R.id.imageView1)
//show it in a imageview with glide with the url
Glide.with(this@MapsActivity).load(urifinal).into(imageView)
imageView.visibility = View.VISIBLE
} else {
// Handle failures
Toast.makeText(this, "An error occur", Toast.LENGTH_SHORT).show()
// ...
}
}
}
获取下载URL的关键点在于理解有多个任务值用于最终获取实际Uri dbRef.downloadUrl实际上返回一个任务本身。我们从这个任务中检索上传文件的Uri 下面是一个伪代码:
val finalStoragePathRef = mStorageReference.child(selectedImageUri!!.lastPathSegment!!)
//putFile() returns an UploadTask and you can call the listener methods on this task
finalStoragePathRef.putFile(selectedImageUri)
.addOnFailureListener {
//Failure
}.addOnSuccessListener { //If upload was a success, get the downloadUrl
//downloadUrl() returns a Task with the Uri
//Extract the Uri from addOnCompleteListener method of the downloadUrl() method
finalStoragePathRef.downloadUrl.addOnCompleteListener {
val url = it.result
Log.i (TAG, "URL: " + url.toString ())
//Create your data model
val message = Message(...,mUsername,url.toString())
//Add the data to your database
mMessagesDatabaseReference.push().setValue(message)
}
}
希望这是有意义的。许多旧方法现在已被弃用,最好参考文档以获得更好的想法:
获取下载URL的关键是要了解,有多个任务值用于最终获取实际Uri dbRef.downloadUrl实际上返回一个任务本身。我们从这个任务中检索上传文件的Uri 下面是一个伪代码:
val finalStoragePathRef = mStorageReference.child(selectedImageUri!!.lastPathSegment!!)
//putFile() returns an UploadTask and you can call the listener methods on this task
finalStoragePathRef.putFile(selectedImageUri)
.addOnFailureListener {
//Failure
}.addOnSuccessListener { //If upload was a success, get the downloadUrl
//downloadUrl() returns a Task with the Uri
//Extract the Uri from addOnCompleteListener method of the downloadUrl() method
finalStoragePathRef.downloadUrl.addOnCompleteListener {
val url = it.result
Log.i (TAG, "URL: " + url.toString ())
//Create your data model
val message = Message(...,mUsername,url.toString())
//Add the data to your database
mMessagesDatabaseReference.push().setValue(message)
}
}
希望这是有意义的。许多旧方法现在已被弃用,最好参考文档以获得更好的想法:
使用Kotlin
官方文件中有一点错误
但它是用Kotlin纠正的
官方文件中有一点错误
但是已经更正了我可以使用旧版本的firebase存储吗?例15.0.2?这个版本更容易使用,API更容易理解。我需要更新我的代码,因为我和你有同样的问题,它突然停止工作。现在获取下载URL有点复杂,但这是我知道的唯一方法。我建议你更新,因为这也带来了改进和错误修复。我可以使用旧版本的firebase存储吗?例15.0.2?这个版本更容易使用,API更容易理解。我需要更新我的代码,因为我和你有同样的问题,它突然停止工作。现在获取下载URL有点复杂,但这是我知道的唯一方法。我建议您进行更新,因为这也会带来改进和错误修复。val download=task.result返回任务val download=task.result返回任务