[英]how to retrieve images already uploaded to Firebase on android dynamically?
我已将 2 张图像上传到 Firebase image1.PNG和image2.PNG ,然后我检索它们并在 imageview1 和 imageview2 中显示它们
private ImageView imageview1;
private ImageView imageview2;
imageview1=(ImageView)findViewById(R.id.imageview1);
imageview2=(ImageView)findViewById(R.id.imageview1);
String url1="https://firebase/khtvuii";
String url2="https://firebase/kljgffh";
Glide.with(getApplicationContext()).load(url1).into(imageview1);
Glide.with(getApplicationContext()).load(url2).into(imageview2);
我不想获取每个图像的 url 并将其粘贴到我的代码中,我想在 firebase 存储中上传图像,然后这些图像 url 将直接添加到我的代码中,比如制作一个 arrayList 以从每个 firebase 数据中获取 url我在数据库中添加 url 的时间将添加到我的列表中并显示在我的 recyclerview 或 imageview 等中。
请帮帮我伙计们
这是你应该做的。 如果您需要任何解释,请告诉我。
int count;
HorizontalScrollView horizontalScrollView;
LinearLayout linearLayout;
RelativeLayout rl;
List<String> imagesUrl = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Your DB with images
alpha = FirebaseDatabase.getInstance().getReference().child("alpha");
// to put horizontalscroll
rl = findViewById(R.id.RL);
alpha.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
count = (int) dataSnapshot.getChildrenCount();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
imagesUrl.add(ds.getValue().toString());
Log.i("da", imagesUrl.toString());
// Create new horizontal scroll view with imageViews
// There could be any number of images that's why it is better to make programmatically
horizontalScrollView = new HorizontalScrollView(MainActivity.this);
linearLayout = new LinearLayout(MainActivity.this);
ViewGroup.LayoutParams prams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(prams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalScrollView.addView(linearLayout);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// I showed all images on a button click you can do it on start or on create
public void show(View view) {
int c = 0;
ImageView[] imageViews = new ImageView[count +1 ];
while (c < count) {
imageViews[c] = new ImageView(MainActivity.this);
imageViews[c].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
imageViews[c].setPadding(10,0,10,0);
Log.i("image", imagesUrl.get(c).toString());
Picasso.with(getApplicationContext()).load(imagesUrls.get(c)).into(imageViews[c]);
linearLayout.addView(imageViews[c]);
c++;
}
rl.addView(horizontalScrollView);
}}
更新答案
从图库中选择多张图片上传后的OnActivityresult
if(requestCode == SELECT_PICTURES) {
if (resultCode == MainActivity.RESULT_OK) {
if (data.getClipData() != null) {
count = data.getClipData().getItemCount();
Log.i("count", String.valueOf(count));
int currentItem = 0;
while (currentItem < count) {
imageUri = data.getClipData().getItemAt(currentItem).getUri();
Log.i("uri", imageUri.toString());
String img = imageUri.toString();
mArrayUri.add(imageUri);
currentItem = currentItem + 1;
}
Log.i("listsize", String.valueOf(mArrayUri.size()));
// Create new horizontal scoll view with imageview to show selected images
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams prams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(prams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalScrollView.addView(linearLayout);
// Define as member
// private ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
int b = mArrayUri.size();
int c = 0;
ImageView[] imageViews = new ImageView[mArrayUri.size()];
while (c < mArrayUri.size()) {
imageViews[c] = new ImageView(this);
imageViews[c].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
imageViews[c].setPadding(10,0,10,0);
imageViews[c].setImageURI(mArrayUri.get(c));
linearLayout.addView(imageViews[c]);
c++;
}
mImageHolder.addView(horizontalScrollView);
} else if (data.getData() != null) {
String imagePath = data.getData().getPath();
//do something with the image (save it to some directory or whatever you need to do with it here)
}
}
}//****showing user image he selected ends here****//
// Uploading images to storage after getting result
while (up < mArrayUri.size()){
// fiLe path is the storage folder in which you want to put images
filepath.child.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
Log.i("downloadurl", downloadURL.toString());
// Define as member
// private ArrayList<Uri> downstring = new ArrayList<>();
downstring.add(downloadURL);
}
});
up++;
k++;
}
** 将图像以 img1、img2 等格式保存到数据库中**
/// Create Save button or do it on activity result
int l = 0;
while (l < downstring.size()){
Map n = new HashMap();
// countingImage is an int with value 0 , defined as member.
n.put(String.valueOf("img" + countingImage), downstring.get(l).toString());
// pushing n Map for Images
// DB will be your image node
DB.child(pushKey).child("images").updateChildren(n).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Toast.makeText(YourActivity.this, "got download url", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(YourActivity.this, "Failed to put in db", Toast.LENGTH_SHORT).show();
}
}
});
l++;
countingImage++;
}
上面的代码将让您从图库中选择多个图像,并将它们以 img1、img2 等序列保存到数据库中。
现在显示来自 url 的图像
获取该图像节点的子节点数。 并以编程方式使 imageviews 数组等于 children 计数,如上面的代码所示。
然后使用循环,假设孩子数是k
下图 addValueEventListener
Int i = 0;
While (i <= k) {
Picasso.with(context).load(datasnapsho.child("img" + i).getValue.toString.into(imageView[i])
i++;
}
这将创建所需的图像视图并自动用 url 填充它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.