图像从firebase获取到'my profile'的imageview

时间:2018-05-03 12:26:18

标签: android firebase-realtime-database

目前,我正在构建一个应用程序。在此应用程序中,我希望用户选择并将配置文件图像上载到数据库中。上传后,用户应该能够将他的个人资料图像查看到“我的个人资料”页面中给出的imageView中。我已经完成了选择和上传图像但用户的任务。主要问题是我无法弄清楚图像应该如何显示在“我的个人资料”的imageView中。

以下是“我的个人资料”页面的代码:

//the code starts here

public class myprofile extends Signin implements View.OnClickListener {

    TextView u_name, u_usn, u_year, u_branch, u_email;
    ImageView imageView;
    private Uri filePath;
    private final int PICK_IMAGE_REQUEST = 71;
    private FirebaseAuth firebaseAuth;
    DatabaseReference databaseStudents;

    private static final String TAG = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getOverflowMenu();

        setContentView(R.layout.activity_myprofile);
        u_name = (TextView) findViewById(R.id.name_myprofile);
        u_usn = (TextView) findViewById(R.id.usn_myprofile);
        u_year = (TextView) findViewById(R.id.year_myprofile);
        u_branch = (TextView) findViewById(R.id.branch_myprofile);
        u_email = (TextView) findViewById(R.id.email_myprofile);
        imageView = (ImageView) findViewById(R.id.imageView);
        //img fetch


        firebaseAuth = FirebaseAuth.getInstance();
        final FirebaseUser currentUser = firebaseAuth.getCurrentUser();

        databaseStudents = FirebaseDatabase.getInstance().getReference("students");
        databaseStudents.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    if (currentUser.getEmail().equalsIgnoreCase(ds.getValue(user.class).getEmail())) {
                        user userInfo = ds.getValue(user.class);
                        u_name.setText(userInfo.name);
                        u_usn.setText(userInfo.usn);
                        u_year.setText(userInfo.year);
                        u_email.setText(userInfo.email);
                        u_branch.setText(userInfo.branch);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


        netConnection checkCon = new netConnection(this);
        if (checkCon.isConnected() == false) {
            new AlertDialog.Builder(this)
                    .setTitle("WARNING")
                    .setMessage("No Internet Connection!!\n\nEnable it..!!!")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent intent = new Intent(Settings.ACTION_SETTINGS);
                            startActivity(intent);
                        }
                    })
                    .show();
        }
    }

    private void getOverflowMenu() {

        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用 Glide 库从任何地方加载图像。

首先您想要附加的文档 HERE

添加Glide Dependancy

依赖性是:实施' com.github.bumptech.glide:glide:4.7.1'

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.7.1'
}

在build.gradle(模块:应用)应用级文件

中添加以上行

然后更新java文件中的以下代码

ImageView imageView = (ImageView) findViewById(R.id.imageView);

for (DataSnapshot ds : dataSnapshot.getChildren()) {
    if (currentUser.getEmail().equalsIgnoreCase(ds.getValue(user.class).getEmail())) {
        user userInfo = ds.getValue(user.class);
        u_name.setText(userInfo.name);
        u_usn.setText(userInfo.usn);
        u_year.setText(userInfo.year);
        u_email.setText(userInfo.email);
        u_branch.setText(userInfo.branch);

        Glide.with(context)
             .asBitmap()
             .load(userInfo.imageVariableName)
             .into(imageView);
    }
}