关于Firebase和imageView的问题

huangapple go评论113阅读模式
英文:

I have a question about firebase and imageView

问题

我有一个关于包含Url图像的对象的问题,我需要从Firebase实时数据库中获取这些对象,然后从中获取Url并将它们插入到创建imageView并与图像一起添加到linerleaut的方法中。一切都对我有效,我获取了所有的url对象,但屏幕上的imageView显示的是最后添加的图像,我仍然可以从应用程序中添加照片,我尝试了Picasso和Bitmap,但它不起作用。

数据库引用.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {

        linearLayoutShirt.removeAllViews();
        linearLayoutPant.removeAllViews();
        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            Image object = postSnapshot.getValue(Image.class);
            linearLayoutShirt.addView(test(dress, 300, 300));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(MyActivity.this, "读取失败:" + databaseError.getMessage(), Toast.LENGTH_LONG).show();
    }
});

public LinearLayout test(final Image image, int weight, int height) {
    LinearLayout infoView = new LinearLayout(MyActivity.this);
    LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    infoView.setPadding(30, 30, 30, 30);

    infoView.setOrientation(LinearLayout.VERTICAL);
    infoView.setLayoutParams(infoViewParams);

    ImageView infoImageView = new ImageView(MyActivity.this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(weight, height);
    infoImageView.setLayoutParams(params);

    new getImageFromUrl(infoImageView).execute(image.getImage());

    /*//Picasso.get().load(image.getImage()).into(infoImageView);

    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(dress.getImage()));
        infoImageView.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }*/

    infoView.addView(infoImageView);

    return infoView;
}

[![图像描述][1]][1]


请注意,我已保留了您的原始代码的结构,但是我注释掉了一些与图像加载相关的部分。您可以根据需要启用Picasso或Bitmap加载图像的部分,以确保imageView显示正确的图像。
<details>
<summary>英文:</summary>
I have such a problem with objects in which there is an Url image, and I need to take these objects from firebase realtimeDatabase and take Url from them and insert their method that creates the imageView and adds with the image to the linerleaut, everything works for me, I take everything all url objects I take but on the screen the imageView appears with the last added image, de still I can add a photo from the application,i tried Picasso and Bitmap but it doesn&#39;t work.
enter code here
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
linearLayoutShirt.removeAllViews();
linearLayoutPant.removeAllViews();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Image object = postSnapshot.getValue(Image.class);
linearLayoutShirt.addView(test(dress, 300, 300));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MyActivity.this, &quot;The read failed: &quot; + databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
public LinearLayout test(final Image image, int weight, int height) {
LinearLayout infoView = new LinearLayout(MyActivity.this);
LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
infoView.setPadding(30, 30, 30, 30);
infoView.setOrientation(LinearLayout.VERTICAL);
infoView.setLayoutParams(infoViewParams);
ImageView infoImageView = new ImageView(MyActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(weight, height);
infoImageView.setLayoutParams(params);
new getImageFromUrl(infoImageView).execute(image.getImage());
/*//Picasso.get().load(image.getImage()).into(infoImageView);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(dress.getImage()));
infoImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}*/
infoView.addView(infoImageView);
return infoView;
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/VdACG.jpg
</details>
# 答案1
**得分**: 0
如您在评论中所述,您收到以下错误消息:
{ error: { code: 403, message: "Permission denied. Could not perform this operation" } }
然后,请转到您的 Firebase 数据库规则,您将找到以下规则:
{
/* 访问https://firebase.google.com/docs/database/security以了解有关安全规则的更多信息。 */
"rules": {
".read": false,
".write": false //建议将此规则设置为request.auth.uid != null,以确保没有人可以访问修改您的数据
}
}
现在根据您的需求更改权限,例如,如果您设置为 `true`,则任何人都可以从任何平台查看图像,如果您设置为 `request.auth.uid != null`,则只有您应用程序的注册用户可以查看图像。
<details>
<summary>英文:</summary>
As you said in your comment you&#39;re getting the following 
{ error: { code: 403, message: &quot;Permission denied. Could not perform this operation&quot; } }
Then go to you&#39;re firebase database rules you&#39;ll find the following rules 
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
&quot;rules&quot;: {
&quot;.read&quot;: false,
&quot;.write&quot;: false //It is advised to set this rule as request.auth.uid != null to ensure no one will access to modify your data
}
}
Now change the permission&#39;s according to your need like if you set `true` then anyone with the link can see the image from any platform and if you set `request.auth.uid != null` then only a registered user of your app can see the image.
</details>

huangapple
  • 本文由 发表于 2020年7月28日 02:35:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定