如何动态检索已经上传到Firebase的图像?

时间:2018-02-11 00:14:42

标签: android firebase firebase-realtime-database

我已将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);

我不想获取每个图像的网址并将其粘贴到我的代码中,我想在firebase存储中上传图像然后这些图片网址将直接添加到我的代码中,就像创建一个arrayList来获取网址每次我在数据库中添加url时,firebase数据都会添加到我的列表中,并显示在我的recyclerview或imageview等中。

PLZ帮帮我们

2 个答案:

答案 0 :(得分:1)

这是你应该做的。如果您需要任何解释,请告诉我。

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);
    }}

答案 1 :(得分:0)

更新回答

从库中选择多个图像后,

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等的DB中​​。

现在显示来自网址的图片

获取该图像节点的子计数。 并使imageviews数组在程序上等于子计数,如上面的代码所示。

然后使用循环,假设儿童计数为k

在图片addValueEventListener下

      Int i = 0;
    While (i <= k) {

    Picasso.with(context).load(datasnapsho.child("img" + i).getValue.toString.into(imageView[i])
i++;
    }

这将创建所需的图像视图并自动用网址填充它们。