我在登录后将图片和文字上传到firebase。它第一次成功上传了image url
和text
。但是现在我已经登录了,我只是继续上传活动并上传image url
和text
。
新image url
和text
会覆盖同一image url
下的旧text
和uid
。我怎么能避免这个?如何在每次上传uid
和image url
时创建新的text
,以便新的不会覆盖旧的。{/ p>
在此之前我已经上传了2张带文字的图片,但是这个新孩子覆盖了旧图片。
MyUploadService.java
public class MyUploadService extends AppCompatActivity {
Button select_image,upload_button;
ImageView user_image;
TextView title;
public static final int READ_EXTERNAL_STORAGE = 0;
private static final int GALLERY_INTENT = 2;
private ProgressDialog mProgressDialog;
private Firebase mRoofRef;
private FirebaseAuth mAuth;
private Uri mImageUri = null;
private DatabaseReference mdatabaseRef;
private StorageReference mStorage;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseApp.initializeApp(this);
setContentView(R.layout.upload_layout);
Firebase.setAndroidContext(this);
mAuth = FirebaseAuth.getInstance();
select_image = (Button)findViewById(R.id.select_image);
upload_button = (Button)findViewById(R.id.upload_bttn);
user_image = (ImageView) findViewById(R.id.user_image);
title = (TextView) findViewById(R.id.etTitle);
//Initialize the Progress Bar
mProgressDialog = new ProgressDialog(MyUploadService.this);
//Select image from External Storage...
select_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//Check for Runtime Permission
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(getApplicationContext(), "Call for
Permission", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
}
}
else
{
callgalary();
}
}
});
//Initialize Firebase Database paths for database and Storage
mdatabaseRef = FirebaseDatabase.getInstance().getReference();
mRoofRef = new Firebase("https://first-project-
7f5d4.firebaseio.com/").child("Item_Details(categories)").push();
// Push will create new child every time we upload data
mStorage =
FirebaseStorage.getInstance().getReferenceFromUrl("gs://first-
project-7f5d4.appspot.com");
//Click on Upload Button Title will upload to Database
upload_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String mName = title.getText().toString().trim();
if(mName.isEmpty())
{
Toast.makeText(getApplicationContext(), "Fill all Field",
Toast.LENGTH_SHORT).show();
return;
}
Firebase childRef_name = mRoofRef.child("Image_Title");
childRef_name.setValue(mName);
Toast.makeText(getApplicationContext(), "Updated Info",
Toast.LENGTH_SHORT).show();
signInAnonymously();
}
});
}
//Check for Runtime Permissions for Storage Access
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
switch (requestCode) {
case READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED)
callgalary();
return;
}
Toast.makeText(getApplicationContext(), "...",
Toast.LENGTH_SHORT).show();
}
//If Access Granted gallery Will open
private void callgalary() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_INTENT);
}
//After Selecting image from gallery image will directly uploaded to
Firebase Database
//and Image will Show in Image View
@Override
public void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
mImageUri = data.getData();
user_image.setImageURI(mImageUri);
StorageReference filePath =
mStorage.child("Item_Images_By_Category").child
(mImageUri.getLastPathSegment());
mProgressDialog.setMessage("Uploading Image....");
mProgressDialog.show();
filePath.putFile(mImageUri).addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
//Ignore This error
mRoofRef.child("Image_URL").setValue(downloadUri.toString());
Glide.with(getApplicationContext())
.load(downloadUri)
.crossFade()
.placeholder(R.drawable.loading)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(user_image);
Toast.makeText(getApplicationContext(), "Updated.",
Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
});
}
}
private void signInAnonymously() {
final String TAG = "Storage#MainActivity";
// Sign in anonymously. Authentication is required to read or write from
Firebase Storage.
mAuth.signInAnonymously()
.addOnSuccessListener(this, new OnSuccessListener<AuthResult>()
{
@Override
public void onSuccess(AuthResult authResult) {
Log.d(TAG, "signInAnonymously:SUCCESS");
Toast.makeText(getApplicationContext(), "Successful Signin",
Toast.LENGTH_SHORT).show();
updateUI(authResult.getUser());
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e(TAG, "signInAnonymously:FAILURE", exception);
hideProgressDialog();
updateUI(null);
}
});
}
private void updateUI(FirebaseUser user) {
// Signed in or Signed out
if (user != null) {
} else {
}
}
private void showProgressDialog(String caption) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.setMessage(caption);
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
答案 0 :(得分:0)
要解决此问题,您需要使用updateChildren()
方法代替setValue()
方法。由于Firebase的结构为一对键和值,因此在Map
的情况下,它会将旧值替换为新值。
希望它有所帮助。