我要将6张图片上传到Firebase存储,并将图片路径和一些数据保存在Firebase数据库中。到目前为止,仅上传全部6张图像时,该功能仍然有效。
我是BSimagePicker库https://github.com/siralam/BSImagePicker,用于选择多个图像,然后将其上传到存储并在数据库中保存路径。
我遇到的问题是,如果我要上传少于6张图像,则会崩溃,并指出我指向的PUTFILE为空。 我如何确定如果uri为null则可以上传吗?
此外,我处理路径上传的最佳方式是什么?
我找不到有关上传多个文件并将其路径保存到实时数据库的教程。全部只有一个文件。
private void startPosting() {
mPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(ImageLoader.TAG, "onClick: attempting to post...");
if (!isEmpty(mTitle.getText().toString())
&& !isEmpty(mDescription.getText().toString())
&& !isEmpty(mPrice.getText().toString())
&& !isEmpty(mCountry.getText().toString())
&& !isEmpty(mStateProvince.getText().toString())
&& !isEmpty(mCity.getText().toString())
&& !isEmpty(mContactEmail.getText().toString())) {
if (mAllUri != null ) {
Toast.makeText(getActivity(), "uploading image", Toast.LENGTH_SHORT).show();
final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();
StorageReference filepath = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image");
final StorageReference filepath1 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image1");
final StorageReference filepath2 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image2");
final StorageReference filepath3 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image3");
final StorageReference filepath4 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image4");
final StorageReference filepath5 = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image5");
final Uri[] firebaseUri = new Uri[1];
final Uri[] firebaseUri1 = new Uri[1];
final Uri[] firebaseUri2 = new Uri[1];
final Uri[] firebaseUri3 = new Uri[1];
final Uri[] firebaseUri4 = new Uri[1];
final Uri[] firebaseUri5 = new Uri[1];
filepath.putFile(mSelectedUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri[0] = taskSnapshot.getDownloadUrl();
filepath1.putFile(mSelectedUri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri1[0] = taskSnapshot.getDownloadUrl();
filepath2.putFile(mSelectedUri2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri2[0] = taskSnapshot.getDownloadUrl();
filepath3.putFile(mSelectedUri3).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri3[0] = taskSnapshot.getDownloadUrl();
filepath4.putFile(mSelectedUri4).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firebaseUri4[0] = taskSnapshot.getDownloadUrl();
filepath5.putFile(mSelectedUri5).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
firebaseUri5[0] = taskSnapshot.getDownloadUrl();
Post post = new Post();
post.setCity(mCity.getText().toString());
post.setContact_email(mContactEmail.getText().toString());
post.setCountry(mCountry.getText().toString());
post.setDescription(mDescription.getText().toString());
post.setPost_id(postId);
post.setPrice(mPrice.getText().toString());
post.setState_province(mStateProvince.getText().toString());
post.setTitle(mTitle.getText().toString());
post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
post.setImage(firebaseUri[0].toString());
post.setImage1(firebaseUri1[0].toString());
post.setImage2(firebaseUri2[0].toString());
post.setImage3(firebaseUri3[0].toString());
post.setImage4(firebaseUri4[0].toString());
post.setImage5(firebaseUri5[0].toString());
reference.child(getString(R.string.node_posts))
.child(postId)
.setValue(post);
showProgressBar();
resetFields();
}
});
}//file4
});//file4
} //file3
}); //file3
}//file2
}); //file2
}//file1
});//file1
}
});
}
//
}
}
});
}
这是检索uri的函数:
@Override
public void onMultiImageSelected(List<Uri> uriList, String tag) {
for (int i = 0; i < uriList.size(); i++) {
mAllUri = uriList.get(i);
if (i >= 6) return;
ImageView iv;
switch (i) {
case 0:
iv = ivImage1;
mSelectedUri = uriList.get(0);
Log.d(ImageLoader.TAG, "aissa image" + mSelectedUri);
break;
case 1:
iv = ivImage2;
mSelectedUri1 = uriList.get(1);
Log.d(ImageLoader.TAG, "aissa1 image" + mSelectedUri1);
break;
case 2:
iv = ivImage3;
mSelectedUri2 = uriList.get(2);
break;
case 3:
iv = ivImage4;
mSelectedUri3 = uriList.get(3);
break;
case 4:
iv = ivImage5;
mSelectedUri4 = uriList.get(4);
break;
case 5:
default:
iv = ivImage6;
mSelectedUri5 = uriList.get(5);
}
Glide.with(this).load(uriList.get(i)).into(iv);
}
}
答案 0 :(得分:0)
据我对您的问题的了解,您希望采用一种不会在ImageView中放入null的方法。
为此,您可以仅检查从ArrayList检索的项目是否为空,换句话说,只需在将if
变量放入您的mSelectedURi
变量之前就放置一个@Override
public void onMultiImageSelected(List<Uri> uriList, String tag) {
for (int i = 0; i < uriList.size(); i++) {
mAllUri = uriList.get(i);
if (i >= 6) return;
ImageView iv;
switch (i) {
case 1:
iv = ivImage2;
if(uriList.get(1)!=null && !uriList.get(1).equals("")){
mSelectedUri1 = uriList.get(1);
Log.d(ImageLoader.TAG, "aissa1 image" + mSelectedUri1);
}
else
// do something when it is null, like displaying a toast or something
break;
case 2:
// do this for all these cases
}
}
语句。
因此,您最后输入的代码应如下所示:
if($query_run = mysqli_query($link, $query)) {
// This gets you one row at a time, use a while if there are multiple rows
// while($row = mysqli_fetch_assoc($query_run)){}
$row = mysqli_fetch_assoc($query_run);
$count = $row['count'];
// Do whatever with $count
}