How To Create An App That Allows For Profile Picture Upload/Change?

后端 未结 1 687
暗喜
暗喜 2021-02-14 10:39

I have been searching high and low to find an example of how to implement profile picture changes within my app. I want to allow persons to add/change their profile image using

1条回答
  •  梦谈多话
    2021-02-14 11:25

    First, I have to say that this a really well formulated question and I would be happy that every guy with few reputation asks so good formulated questions.

    The problem you encounter is basically an issue with the Android Activity lifecycle. I guess, the problem is really trivial: I see nowhere in your Activity onCreate() a place where you retrieve the image from Parse: your download method is only called in a onClickListener.

    So instead of having it here, I would extract it to a private method, kind of something like this:

    Edit:

       private void queryImagesFromParse(){
            ParseQuery imagesQuery = new ParseQuery<>("User");
            imagesQuery.findInBackground(new FindCallback() {
                @Override
                public void done(List imagesItems, ParseException e) {
                    if(e == null){
    
                        ParseUser userCurrentOfParse = ParseUser.getCurrentUser();
                        if(userCurrentOfParse != null) {
                            //final String imgUrl = imageUploadPassed.getParseFile("imageContent").getUrl();
                            final String imgUrl = userCurrentOfParse.getParseFile("userProfilePics").getUrl();
    
    
                            mHomeProfilePic = (ImageView) findViewById(R.id.userHomeProfilePicImageView);
                            Picasso.with(HomeActivity.this).load(imgUrl).into(mHomeProfilePic);
    
                            //imageUploadPassed.pinInBackground();
    
                           // profileImageId = imageUploadPassed.getObjectId();
                            //Log.d(TAG, "The object id is: " + profileImageId);
                        }
    
                    }else{
                        Toast.makeText(HomeActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    

    (The code above is just to give an rough idea, I would be surprised if it compiles).

    And then, you call this method at the end of the onCreate() (onStart could work as well, but I'd prefer onCreate()). Of course, you can call this method also from the place where it was before (that is actually what happens if you literally extract the method Right-Click > Refractor > Extract Method)

    Btw, very good that you use Picasso but it could be better to initialise it with the Context of your Activity so Picasso.with(MainActivity.this).load(imgUrl).into(mProfilePic); instead of Picasso.with(getApplicationContext()).load(imgUrl).into(mProfilePic); (should be 1 ns faster!)

    Edit: Also be sure that the image is being upload and queried from the User table on Parse this will ensure that each user will see their own image(currently logged in user's image) and not that of every other user that uploads the next image.

    Hope it helps!

    0 讨论(0)
提交回复
热议问题