图片未使用毕加索在应用中更新

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片未使用毕加索在应用中更新相关的知识,希望对你有一定的参考价值。

当我尝试更新我的应用程序中的图像时,它没有更新,但在firebase存储更新中我被使用

。Picasso.get()负载(图像).placeholder(R.drawable.profile).into(profileImage);

我也尝试过

Picasso.with(SetupActivity.class).load(图像).placeholder(R.drawable.profile).into(profileImage);

这些是我的依赖

    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-storage:16.0.1'

    implementation 'de.hdodenhof:circleimageview:2.2.0'

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'

    implementation 'com.squareup.picasso:picasso:2.71828'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
public class SetupActivity extends AppCompatActivity {


    private EditText userName, fullName, countryName;
    private Button saveInformationButton;
    private CircleImageView profileImage;

    private FirebaseAuth mAuth;
    private DatabaseReference userRef;
    private StorageReference userProfileImageRef;


    private ProgressDialog loadingBar;


    String currentUserID;
    final  static  int Gallery_Pick = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);

        getSupportActionBar().setTitle("User Details");


        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
        userRef = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);

        userProfileImageRef = FirebaseStorage.getInstance().getReference().child("Profile Images");

        userName =(EditText)findViewById(R.id.setup_username);
        fullName =(EditText)findViewById(R.id.setup_full_name);
        countryName =(EditText)findViewById(R.id.setup_country_name);
        saveInformationButton = (Button)findViewById(R.id.setup_information_button);
        profileImage = (CircleImageView)findViewById(R.id.setup_profile_image);

        loadingBar = new ProgressDialog(this);


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

        profileImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, Gallery_Pick);

            }
        });

        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                if(dataSnapshot.exists())
                {
                    if (dataSnapshot.hasChild("profileimage"))
                    {
                        String image = dataSnapshot.child("profileimage").getValue().toString();
                        Picasso.get().load(image).placeholder(R.drawable.profile).into(profileImage);
                    }
                    else
                    {
                        Toast.makeText(SetupActivity.this, "Please select profile image first.", Toast.LENGTH_SHORT).show();
                    }
                }


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }




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

        if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
        {
            Uri ImageUri = data.getData();

            CropImage.activity()
                    .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("Profile Image");
                loadingBar.setMessage("Please wait, while we updating your profile image...");
                loadingBar.show();
                loadingBar.setCanceledOnTouchOutside(true);

                Uri resultUri = result.getUri();

                StorageReference filePath = userProfileImageRef.child(currentUserID + ".jpg");

                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
                    {
                        if(task.isSuccessful())
                        {
//                            Toast.makeText(SetupActivity.this, "Profile Image updated...", Toast.LENGTH_SHORT).show();

                            final String downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();

                            userRef.child("profileimage").setValue(downloadUrl)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task)
                                        {
                                            if(task.isSuccessful())
                                            {
                                                Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
                                                startActivity(selfIntent);

                                                Toast.makeText(SetupActivity.this, "Profile Image Updated...", Toast.LENGTH_SHORT).show();
                                                loadingBar.dismiss();
                                            }
                                            else
                                            {
                                                String message = task.getException().getMessage();
                                                Toast.makeText(SetupActivity.this, "Error Occured: " + message, Toast.LENGTH_SHORT).show();
                                                loadingBar.dismiss();
                                            }
                                        }
                                    });
                        }
                    }
                });
            }
            else
            {
                Toast.makeText(this, "Error Occured: Image not uploaded, Try Again.", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();
            }
        }
    }
答案

在毕加索试试这个.memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE)

另一答案

Picasso会缓存图像,但如果您的图像有不同的URI,则会下载它,但前一个图像仍然被缓存,您可以“无效”删除前一个图像,然后加载最近的图像。

Picasso.with(bContext).invalidate(bFileURI); //invalidates files

如果您的结构是使用相同的URI覆盖图像,并且您根本不希望它缓存,那么请继续

Picasso.with(bContext) .load(bImage_url) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.NO_CACHE) .placeholder(R.drawable.default_img) .into(bImageView);

(我宁愿失效,因为后者不是离线使用的好习惯)

由于您使用的是Firebase,因此您也可以考虑这一点,并将该字节保存为SharePreference中的字符串以供离线使用......不良做法仍然存在。

final long bOneMB = 1024 * 1024;                               //size 
userProfileImageRef.getBytes(bOneMB)                           //get byte
    .addOnSuccessListener(new OnSuccessListener<byte[]>() {       // add listener
        @Override
        public void onSuccess(byte[] bytes) {                     // called if successful
            Bitmap bBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
            profileImage.setImageBitmap(bBitmap);                // sets bitmap in image view
        }
    });

以上是关于图片未使用毕加索在应用中更新的主要内容,如果未能解决你的问题,请参考以下文章

如何清理毕加索的内存缓存?

从数据库成功检索数据后,医疗 ID 片段未更新

GMSMarker 信息窗口内容(片段)未更新

个人资料图片上传器并使用毕加索加载

应用程序中的图像未更新

Picasso(毕加索)加载圆形图片圆角图片