更新个人资料图片

时间:2018-06-23 08:40:38

标签: java android

所以基本上,自从更新gradle设置以来,我一直无法更改应用程序配置文件中的配置文件图片。

当我单击图像时,应该让我从存储的图像中选择一个新图片,但确实如此,但是当我单击图像时,它会尝试更改配置文件,但没有更改。我也将它链接到Firebase,并且确实将其上传到其中,因此我确实知道该部分工作正常,只是该部分应该在无法使用的配置文件上更改图像。

上传到firebase的代码:

 private void uploadImageToFirebaseStorage() {
    StorageReference profileImageRef =
            FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");

    if (uriProfileImage != null) {
        progressBar.setVisibility(View.VISIBLE);
        profileImageRef.putFile(uriProfileImage)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressBar.setVisibility(View.GONE);
                        profileImageUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(ProfileActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

更改个人资料图片的代码:

 private void showImageChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Profile Image"), CHOOSE_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
        uriProfileImage = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriProfileImage);
            imageView.setImageBitmap(bitmap);

            uploadImageToFirebaseStorage();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

图片代码:

imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showImageChooser();
        }
    });

    loadUserInformation();

等级:

dependencies {

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:customtabs:27.1.1'

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-vector-drawable:27.1.1'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'


implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'

implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.google.android.gms:play-services-ads:15.0.1'
implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.android.gms:play-services-gcm:15.0.1'


implementation 'com.rengwuxian.materialedittext:library:2.1.4'
implementation 'com.firebase:firebase-client-android:2.5.2'

implementation 'com.firebaseui:firebase-ui-database:3.3.1'
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'

implementation 'com.android.support:multidex:1.0.3'

1 个答案:

答案 0 :(得分:0)

尝试一下,在onActivityResult方法内添加此代码,然后尝试

Uri resultUri = data.getData();
StorageReference filepath = FirebaseStorage.getInstance().getReference().child("profile_images").child(System.currentTimeMillis() + ".jpg");

filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {

public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(AddDeviceActivity.this, "Your Image Uploaded!", Toast.LENGTH_SHORT).show();
                        final String downloadUrl = task.getResult().getDownloadUrl().toString();
                        UserRef.child("profileimage").setValue(downloadUrl)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if(task.isSuccessful()){
                                            //when success
                                        }else {
                                            //when failed

                                        }
                                    }
                                });
                    }
                }