FireStorage Java Android 保存图片到FireStorage需要上传两次
FireStorage Java Android Saving the image to the FireStorage requires uploading twice
我正在使用 Firestore 开发一个 post 系统,并使用 Firebase 存储来托管图像。用户必须填写 5 个字段,其中之一是要添加的图像。所以,为了让用户 select 图像,我这样做:
img_imgpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Defining Implicit Intent to mobile gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(
intent,
"Select Image from here..."),
PICK_IMAGE_REQUEST);
}
});
protected void onActivityResult(int requestCode,
int resultCode,
Intent data)
{
super.onActivityResult(requestCode,
resultCode,
data);
// checking request code and result code
// if request code is PICK_IMAGE_REQUEST and
// resultCode is RESULT_OK
// then set image in the image view
if (requestCode == PICK_IMAGE_REQUEST
&& resultCode == RESULT_OK
&& data != null
&& data.getData() != null) {
// Get the Uri of data
ImageUri = data.getData();
try {
// Setting image on image view using Bitmap
Bitmap bitmap = MediaStore
.Images
.Media
.getBitmap(
getContentResolver(),
ImageUri);
img_imgpost.setImageBitmap(bitmap);
}
catch (IOException e) {
// Log the exception
e.printStackTrace();
}
}
}
此时,当用户点击创建post按钮时,应用会上传图片,然后上传用户在字段中输入的数据。所以在 firestore 中我们将有:
|
|
--- title: "Post Title"
|
--- date: September 03, 2018 at 6:16:58 PM UTC+3
|
--- valutation: 8
|
--- urlImage: https// etc
[...] etc
问题是如果不执行两次“create post”按钮的 onclick 事件,我就无法加载图像,事实上,如果您按一次“create post”, firestore 中的 UrlImage 记录将为空,并且不会在 firestore 上加载任何图像。
OnClick 按钮“创建 post”:
btn_invia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
postId = "";
String [] charsForId = new String[] {"a", "b", "c", "d", "e", "f", "z", "3", "*", "#", "%", "&", "^", "r", "!", "j", "h", "q"}; //18 chars
for(int i = 0; i<20; i++){
postId += charsForId[(int) Math.floor((Math.random() * 17) + 0)];
}
Toast.makeText(posta.this, postId, Toast.LENGTH_SHORT).show();
descriz = descr.getText().toString();
titolo = title.getText().toString();
valut = vote.getText().toString();
uploadImage();
uploadDataOnFirestore(postId, descriz, titolo, valut, genre);
}
});
谁能帮我解决这个问题?我把 activity 的完整代码留给你,以防我在描述我的代码时不清楚:https://codeshare.io/eV9zE4
uploadImage()
方法似乎是 运行 异步的,uploadDataOnFirestore
是 运行 紧跟在 uploadImage
之后,无需等待图像上传, 因此图像是 null
.
要解决此问题,请将 uploadDataOnFirestore
方法调用移动到 uploadImage 任务的 OnCompleteListener
onComplete
方法中,以便图像在 运行ning 之前完成上传uploadDataOnFirestore
我正在使用 Firestore 开发一个 post 系统,并使用 Firebase 存储来托管图像。用户必须填写 5 个字段,其中之一是要添加的图像。所以,为了让用户 select 图像,我这样做:
img_imgpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Defining Implicit Intent to mobile gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(
intent,
"Select Image from here..."),
PICK_IMAGE_REQUEST);
}
});
protected void onActivityResult(int requestCode,
int resultCode,
Intent data)
{
super.onActivityResult(requestCode,
resultCode,
data);
// checking request code and result code
// if request code is PICK_IMAGE_REQUEST and
// resultCode is RESULT_OK
// then set image in the image view
if (requestCode == PICK_IMAGE_REQUEST
&& resultCode == RESULT_OK
&& data != null
&& data.getData() != null) {
// Get the Uri of data
ImageUri = data.getData();
try {
// Setting image on image view using Bitmap
Bitmap bitmap = MediaStore
.Images
.Media
.getBitmap(
getContentResolver(),
ImageUri);
img_imgpost.setImageBitmap(bitmap);
}
catch (IOException e) {
// Log the exception
e.printStackTrace();
}
}
}
此时,当用户点击创建post按钮时,应用会上传图片,然后上传用户在字段中输入的数据。所以在 firestore 中我们将有:
|
|
--- title: "Post Title"
|
--- date: September 03, 2018 at 6:16:58 PM UTC+3
|
--- valutation: 8
|
--- urlImage: https// etc
[...] etc
问题是如果不执行两次“create post”按钮的 onclick 事件,我就无法加载图像,事实上,如果您按一次“create post”, firestore 中的 UrlImage 记录将为空,并且不会在 firestore 上加载任何图像。
OnClick 按钮“创建 post”:
btn_invia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setVisibility(View.VISIBLE);
postId = "";
String [] charsForId = new String[] {"a", "b", "c", "d", "e", "f", "z", "3", "*", "#", "%", "&", "^", "r", "!", "j", "h", "q"}; //18 chars
for(int i = 0; i<20; i++){
postId += charsForId[(int) Math.floor((Math.random() * 17) + 0)];
}
Toast.makeText(posta.this, postId, Toast.LENGTH_SHORT).show();
descriz = descr.getText().toString();
titolo = title.getText().toString();
valut = vote.getText().toString();
uploadImage();
uploadDataOnFirestore(postId, descriz, titolo, valut, genre);
}
});
谁能帮我解决这个问题?我把 activity 的完整代码留给你,以防我在描述我的代码时不清楚:https://codeshare.io/eV9zE4
uploadImage()
方法似乎是 运行 异步的,uploadDataOnFirestore
是 运行 紧跟在 uploadImage
之后,无需等待图像上传, 因此图像是 null
.
要解决此问题,请将 uploadDataOnFirestore
方法调用移动到 uploadImage 任务的 OnCompleteListener
onComplete
方法中,以便图像在 运行ning 之前完成上传uploadDataOnFirestore