使用 Picasso 更新 Firebase 数据库中的图像视图



im 编码新手:我正在创建一个体能训练应用,并在将用户个人资料图片存储到 Firebase 数据库后尝试更新用户个人资料图片我尝试使用毕加索和滑翔,无论我使用什么,它都会产生相同的结果......图像视图已保存到Firebase中,但是当它必须更新图像视图时,没有任何反应...

我什至尝试更改Firebase存储和数据库规则。请帮忙

这里的代码清单:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".Login" />
        <activity android:name=".SetupActivity"></activity>
        <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
            android:theme="@style/Base.Theme.AppCompat"/>
    </application>

setup_layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SetupActivity"
    android:background="@drawable/setup_background">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:src="@drawable/profile21"></de.hdodenhof.circleimageview.CircleImageView>
        <!--app:civ_border_color="@color/colorPrimaryDark"-->
        <!--app:civ_border_width="1dp"/>-->
    <EditText
        android:id="@+id/setup_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="237dp"
        android:drawableLeft="@drawable/username"
        android:ems="10"
        android:hint="User Name"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textColorHint="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/setup_fullname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/setup_username"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:drawableLeft="@drawable/name"
        android:ems="10"
        android:hint="Full Name"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textColorHint="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/setup_country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/setup_fullname"
        android:layout_alignParentStart="true"
        android:drawableLeft="@drawable/country"
        android:ems="10"
        android:hint="Country"
        android:inputType="textMultiLine"
        android:textColor="#ffffff"
        android:textColorHint="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="bold" />
    <Button
        android:id="@+id/setup_saveinformation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_marginBottom="62dp"
        android:text="Save"
        android:background="@drawable/button"/>
</RelativeLayout>

设置活动:

public class SetupActivity extends AppCompatActivity {
    private Button Save;
    private EditText Username,Userfullname,Usercountry;
    private CircleImageView profileimage;
    private FirebaseAuth mAuth;
    private DatabaseReference userRef;
    private String currentuserid;
    private ProgressDialog loadingbar;
    private static int GALLARY_PICK = 1;
    private StorageReference Userprofileimageref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        mAuth=FirebaseAuth.getInstance();
        currentuserid=mAuth.getCurrentUser().getUid();
        userRef =FirebaseDatabase.getInstance().getReference().child("Users").child(currentuserid);
        Userprofileimageref = FirebaseStorage.getInstance().getReference().child("profile image");
        Save= findViewById(R.id.setup_saveinformation);
        Username= findViewById(R.id.setup_username);
        Userfullname= findViewById(R.id.setup_fullname);
        Usercountry= findViewById(R.id.setup_country);
        profileimage= findViewById(R.id.profile_image);
        loadingbar = new ProgressDialog(this);
        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveusersetupinfirmation();
            }
        });
        profileimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, GALLARY_PICK);
            }
        });
        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists())
                {
                    String image =dataSnapshot.child("profileimage").getValue().toString();
//
//                    Glide.with(SetupActivity.this)
//                            .load(image)
//                            .into(profileimage);

//                    Picasso.with(SetupActivity.this)
//                            .load(image)
//                            .fit()
//                            .placeholder(R.drawable.profile)
//                            .memoryPolicy(MemoryPolicy.NO_CACHE)
//                            .networkPolicy(NetworkPolicy.NO_CACHE)
//                            .into(profileimage);
                    Picasso.get().load(image).resize(100,100).centerCrop().placeholder(R.drawable.profile).into(profileimage);

                    }

            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==GALLARY_PICK && resultCode== RESULT_OK && data!=null)
        {
            Uri imageuri = data.getData();
            CropImage.activity(imageuri)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1, 1)
                    .start(this);
        }
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                loadingbar.setTitle("saving image");
                loadingbar.setMessage("please wait ....");
                loadingbar.show();
                loadingbar.setCanceledOnTouchOutside(true);
                Uri resultUri = result.getUri();
                final StorageReference filePath = Userprofileimageref.child(currentuserid + "jpg");
                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(Task<UploadTask.TaskSnapshot> task) {
                        if (task.isSuccessful())
                        {
                            Toast.makeText(SetupActivity.this,"Profile image stored successfully",Toast.LENGTH_SHORT).show();
                            final String downloadUrl = filePath.getDownloadUrl().toString();
                            userRef.child("profileimage").setValue(downloadUrl)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(Task<Void> task) {
                                            if (task.isSuccessful())
                                            {
                                                Intent selfintent = new Intent(SetupActivity.this,SetupActivity.class);
                                                startActivity(selfintent);
                                                Toast.makeText(SetupActivity.this,"image saved successfully",Toast.LENGTH_SHORT).show();
                                                loadingbar.dismiss();
                                            }
                                            else
                                                {
                                                    String message = task.getException().getMessage();
                                                    Toast.makeText(SetupActivity.this,"Error occurred: "+ message,Toast.LENGTH_SHORT).show();
                                                    loadingbar.dismiss();
                                            }

                                        }
                                    });
                        }
                    }
                });
            }else
            {
                Toast.makeText(SetupActivity.this,"image cannot be cropped",Toast.LENGTH_SHORT).show();
                loadingbar.dismiss();
            }
        }
    }

请帮忙!!

你必须

将onSuccessListener添加到filePath.getDownloadUrl()。 我在这里添加了代码

final StorageReference storRef = storageReference.child("profilePicture/"+ UUID.randomUUID().toString());
        storRef.putFile(userProfileUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                       storRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                           @Override
                           public void onSuccess(Uri uri) {
                               //Log.d(TAG,"user profile url is: "+uri.toString());
                               saveEmailToFirebase(uri.toString());
                           }
                       }).addOnFailureListener(new OnFailureListener() {
                           @Override
                           public void onFailure(@NonNull Exception e) {
                               Log.d(TAG,"on download porfileuri failed"+e.getMessage());
                               Toast.makeText(LoginActivity.this, "profile picture uploading failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                           }
                       });
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(LoginActivity.this, "profile picture uploading failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                .getTotalByteCount());
                        Log.d(TAG,"File uploading "+(int)progress+"%");
                    }
                });
    }else {
        Toast.makeText(LoginActivity.this,"Please select Profile picture",Toast.LENGTH_SHORT).show();
    }

最新更新