android - 图片上传后Firebase获取网址
问题描述
我有一个应用程序,我想上传两张图片,一张是普通图片,第二张是缩略图。我暂时忽略缩略图,只关注主图像。我正在执行的步骤如下:
第 1 步:上传图片第 2 步:将下载链接作为字符串第 3 步:将下载链接添加到 Firebase 中的实时数据库
我被困在第 2 步,我做了以下事情:
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
{
Uri resultUri = result.getUri();
File thumb_filePath = new File(resultUri.getPath());
Bitmap thumb_bitmap = null;
try {
thumb_bitmap = new Compressor(this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb_filePath);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
final StorageReference mStorageThumbPathRef = mStorageRef.child("chatappthumbimg").child(current_userid + ".jpg");
final StorageReference mStoragePathRef = mStorageRef.child("chatappimg").child(current_userid + ".jpg");
UploadTask uploadTask;
uploadTask = mStoragePathRef.putFile(resultUri);
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 mStoragePathRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
}
}
}
我使用文档寻求帮助:https ://firebase.google.com/docs/storage/android/upload-files
但是,现在我不确定如何进行。mStoragePathRef.getDownloadUrl(); 把图片的真实网址还给我?因为在早期的一些测试中,我得到了某种任务,而不是图像 url
解决方案
根据上面的评论,OP 要求查看我在项目中处理上传的方式 - 不幸的是,这不是 Android。我不认为这会有多大帮助,因为这不是正确的语言,但尽你所能。
具体来说,这是在 Angular 6 中使用 AngularFire2 包完成的。我包括了完整的功能以供参考,但相关部分接近尾声,谈论this.downloadURLObservable
和this.downloadURLSubscription$
// Uploads file to Firebase storage, and returns the file's access URL
pushUpload(pageName, upload) {
// Returns a promise, so we can use .then() when pushUpload is called
return new Promise( (resolve, reject) => {
this.uploadPercent = 0;
// Include the current timeStamp in the file name, so each upload can be uniquely identified - no 1 photo will ever be used in 2 places, can safely delete this file later w/o fear of messing up something else
const timeStamp = new Date().getTime();
// Upload the file
const uploadTask = this.afStorage.upload(`${pageName}/${timeStamp}-${upload.file.name}`, upload.file);
// Observe percentage changes
this.uploadPercentObservable = uploadTask.percentageChanges();
this.uploadPercentageSubscription$ = this.uploadPercentObservable.subscribe(
eachValue => {
this.uploadPercent = Math.round(eachValue*10) / 10
},
err => {
console.log('uploadPercentageSubscription$ errored out in upload.service.ts, here is the err:')
console.log(err)
},
() => {
console.log('uploadPercentageSubscription$ completed')
}
)
// Get notified when the download URL is available, return it from the function
uploadTask.snapshotChanges().pipe( finalize( () => {
this.downloadURLObservable = this.afStorage.ref(`${pageName}/${timeStamp}-${upload.file.name}`).getDownloadURL()
this.downloadURLSubscription$ = this.downloadURLObservable.subscribe(
eachValue => {
resolve(eachValue)
},
err => {
console.log('downloadURLSubscription$ errored out in upload.service..ts, here is the err:')
console.log(err)
},
() => {
console.log('downloadURLSubscription$ completed')
}
)
})).subscribe()
}); // End of returned promise
} // End of pushUpload() for regular image
推荐阅读
- sql - sql oracle获取lib表的所有可能值
- parsing - 在 Haskell 中解析形式逻辑
- unit-testing - 有没有
Gmock 的 EXPECT_CALL 中的动作是什么? - regex - Visual Studio Code Regex 包含但不以
- elasticsearch - 如果在 Elastic-search 中记录了任何 502 或 500 错误,则发送电子邮件警报
- opengl-es - 如何在webgl直方图中对像素进行排序并实现linearGradient?
- azure - Azure Monitor 警报跨资源日志搜索错误
- java - 套接字返回双输入
- python - 如何从 setup.py 添加 cron 作业
- nock - 对多个测试的相同 URL 使用 Nock 失败