Firebase存储中图像的更新

问题描述 投票:1回答:1

我有一个Android应用程序时遇到问题,我想实现插入个人资料照片并将其保存在Firebase存储中的功能,但是当我想用新的照片更新图像时,它仍然是前一张。更新的想法是删除当前的并插入新的。消除和加载阶段很成功,实际上是在Storage中显示了更新的照片,但奇怪的是该应用程序让我看到了旧图像。您对如何解决此问题有任何想法吗?

android firebase firebase-storage
1个回答
0
投票

公共类ImageDetailsActivity扩展了AppCompatActivity {

private static final int PICK_IMAGE_REQUEST = 1;

private Button mButtonChooseImage;
private Button mButtonUpdate;

private TextView key1;

private EditText mEditTextFileNameAr;
private EditText mEditTextFileNameEn;
private EditText mEditTextFileNumberAr;
private EditText mEditTextFileNumberEn;
private EditText mEditTextFileDateAr;
private EditText mEditTextFileDateEn;
private EditText mEditTextFileDateExpAr;
private EditText mEditTextFileDateExpEn;
private EditText mEditTextFileDateRes;
private EditText mEditTextFileType;
private EditText mEditTextFileNationality;
private EditText mEditTextFileTypeBlood;
private EditText mEditTextFileRestrictions;
private ImageView mImageView;
private TextView mTextAddPhoto;
private ImageButton mImageButtonShowUploads;
private ProgressBar mProgressBar;

private String strImage;
private String image;

private Uri mImageUri;

private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;

private StorageTask mUploadTask;

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

    mButtonChooseImage = findViewById(R.id.button_update_image);
    mButtonUpdate = findViewById(R.id.button_update);
    mEditTextFileNameAr = findViewById(R.id.nameAr);
    mEditTextFileNameEn = findViewById(R.id.nameEn);
    mEditTextFileNumberAr = findViewById(R.id.number_id_ar);
    mEditTextFileNumberEn = findViewById(R.id.number_id_en);
    mEditTextFileDateAr = findViewById(R.id.date_ber);
    mEditTextFileDateEn = findViewById(R.id.date_ber_en);
    mEditTextFileDateExpAr = findViewById(R.id.date_exp);
    mEditTextFileDateExpEn = findViewById(R.id.date_exp_en);
    mEditTextFileDateRes = findViewById(R.id.date_res);
    mEditTextFileType = findViewById(R.id.type);
    mEditTextFileNationality = findViewById(R.id.nationality);
    mEditTextFileTypeBlood = findViewById(R.id.type_blood);
    mEditTextFileRestrictions = findViewById(R.id.restrictions);
    mImageView = findViewById(R.id.image_view);
    mTextAddPhoto = findViewById(R.id.AddPhoto);
    mImageButtonShowUploads = findViewById(R.id.home);
    mProgressBar = findViewById(R.id.progress_bar);

    String key = getIntent().getExtras().get("selectedKey").toString();

    mStorageRef = FirebaseStorage.getInstance().getReference().child("uploads").child(key);
    mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("uploads").child(key);

    key1 = findViewById(R.id.key);
    key1.setText(key);

    mEditTextFileNameAr.setText(getIntent().getStringExtra("selectedNameAr"));
    mEditTextFileNameEn.setText(getIntent().getStringExtra("selectedNameEn"));
    mEditTextFileNumberAr.setText(getIntent().getStringExtra("selectedNumberAr"));
    mEditTextFileNumberEn.setText(getIntent().getStringExtra("selectedNumberEn"));
    mEditTextFileDateAr.setText(getIntent().getStringExtra("selectedDateAr"));
    mEditTextFileDateEn.setText(getIntent().getStringExtra("selectedDateEn"));
    mEditTextFileDateExpAr.setText(getIntent().getStringExtra("selectedDateExpAr"));
    mEditTextFileDateExpEn.setText(getIntent().getStringExtra("selectedDateExpEn"));
    mEditTextFileDateRes.setText(getIntent().getStringExtra("selectedDateRes"));
    mEditTextFileType.setText(getIntent().getStringExtra("selectedType"));
    mEditTextFileNationality.setText(getIntent().getStringExtra("selectedNationality"));
    mEditTextFileTypeBlood.setText(getIntent().getStringExtra("selectedTypeBlood"));
    mEditTextFileRestrictions.setText(getIntent().getStringExtra("selectedRestrictions"));

    Intent intent = getIntent();
    strImage = String.valueOf(intent.getStringExtra("selectedImageUri"));
    Picasso.with(this)
            .load(strImage)
            .into(mImageView);


    mImageButtonShowUploads.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openImagesActivity();
        }
    });

    mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openFileChooser();
        }
    });

    mButtonUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    dataSnapshot.getRef().child("mNameAr").setValue(mEditTextFileNameAr.getText().toString());
                    dataSnapshot.getRef().child("mNameEn").setValue(mEditTextFileNameEn.getText().toString());
                    dataSnapshot.getRef().child("mNumberAr").setValue(mEditTextFileNumberAr.getText().toString());
                    dataSnapshot.getRef().child("mNumberEn").setValue(mEditTextFileNumberEn.getText().toString());
                    dataSnapshot.getRef().child("mDateAr").setValue(mEditTextFileDateAr.getText().toString());
                    dataSnapshot.getRef().child("mDateEn").setValue(mEditTextFileDateEn.getText().toString());
                    dataSnapshot.getRef().child("mDateExpAr").setValue(mEditTextFileDateExpAr.getText().toString());
                    dataSnapshot.getRef().child("mDateExpEn").setValue(mEditTextFileDateExpEn.getText().toString());
                    dataSnapshot.getRef().child("mDateRes").setValue(mEditTextFileDateRes.getText().toString());
                    dataSnapshot.getRef().child("mType").setValue(mEditTextFileType.getText().toString());
                    dataSnapshot.getRef().child("mNationality").setValue(mEditTextFileNationality.getText().toString());
                    dataSnapshot.getRef().child("mTypeBlood").setValue(mEditTextFileTypeBlood.getText().toString());
                    dataSnapshot.getRef().child("mRestrictions").setValue(mEditTextFileRestrictions.getText().toString());
                    image = dataSnapshot.child("mImageUrl").getValue().toString();


                    Toast.makeText(ImageDetailsActivity.this, "تم تحديث المعلومات", Toast.LENGTH_LONG).show();
                    ImageDetailsActivity.this.finish();
                }

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

                }
            });
        }
    });

}

private void openFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, PICK_IMAGE_REQUEST);
    mImageView.setVisibility(View.VISIBLE);
}

private String getFileExtension(Uri uri) {
    ContentResolver cR = getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(cR.getType(uri));
}

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null) {
        mImageUri = data.getData();

        Picasso.with(this).load(mImageUri).into(mImageView);
    }

    if (mImageUri != null) {
        final StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() + "." + getFileExtension(mImageUri));

        fileReference.putFile(mImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                        final Task<Uri> downloadUrl = taskSnapshot.getStorage().getDownloadUrl();

                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mProgressBar.setProgress(0);
                            }
                        }, 500);

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


                            }

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

                            }
                        });


                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(ImageDetailsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                        mProgressBar.setProgress((int) progress);
                    }
                });
    } else {
        Toast.makeText(ImageDetailsActivity.this, "No file selected", Toast.LENGTH_SHORT).show();
    }
}

private void openImagesActivity() {
    Intent intent = new Intent(ImageDetailsActivity.this, ImagesActivity.class);
    startActivity(intent);
}
© www.soinside.com 2019 - 2024. All rights reserved.