Reputation: 43
Okay so when I upload the image first time it's fine, but after uploading the second image, the previous gets wiped from the storage. I am liking the code that I am using to upload the image.
I have linked the whole activity class which is responsible for uploading the file to Firebase Storage. have tried to give a different name (uid) to images but to no avail.
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.StorageTask;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import java.util.UUID;
public class ActivityUltimateMemeWorks extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private Button mButtonChooseImage;
private Button mButtonUpload;
private TextView mTextViewShowUploads;
private EditText mEditTextFileName;
private ImageView mImageView;
private ProgressBar mProgressBar;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;
private StorageTask mUploadTask;
private String imageIdentifier;
private Uri mImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ultimate_meme_works);
mButtonChooseImage = findViewById(R.id.button_choose_image);
mButtonUpload = findViewById(R.id.button_upload);
mTextViewShowUploads = findViewById(R.id.text_view_show_uploads);
mEditTextFileName = findViewById(R.id.edit_text_file_name);
imageIdentifier = UUID.randomUUID().toString() + ".png";
mImageView = findViewById(R.id.image_view);
mProgressBar = findViewById(R.id.progress_bar);
mStorageRef = FirebaseStorage.getInstance().getReference("UMWuploads").child("images").child(imageIdentifier);
mDatabaseRef = FirebaseDatabase.getInstance().getReference("UMWuploads");
mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFileChooser();
}
});
mButtonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(ActivityUltimateMemeWorks.this, "Upload in progress", Toast.LENGTH_SHORT).show();
} else {
uploadFile();
}
}
});
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImagesActivity();
}
});
}
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@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.get().load(mImageUri).into(mImageView);
}
}
private void uploadFile() {
if (mImageUri != null) {
mStorageRef.putFile(mImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return mStorageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Toast.makeText(ActivityUltimateMemeWorks.this, "uploading", Toast.LENGTH_SHORT).show();
Log.e("hello", "then: " + downloadUri.toString());
Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
downloadUri.toString());
mDatabaseRef.push().setValue(upload);
} else {
Toast.makeText(ActivityUltimateMemeWorks.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void openImagesActivity() {
Intent intent = new Intent(this, ImagesActivity.class);
startActivity(intent);
}
}
Upvotes: 2
Views: 65
Reputation: 6919
Try this it will change the name of file every time you upload :
private void uploadFile() {
imageIdentifier = UUID.randomUUID().toString() + ".png";
mStorageRef = FirebaseStorage.getInstance().getReference("UMWuploads").child("images").child(imageIdentifier);
if (mImageUri != null) {
mStorageRef.putFile(mImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return mStorageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Toast.makeText(ActivityUltimateMemeWorks.this, "uploading", Toast.LENGTH_SHORT).show();
Log.e("hello", "then: " + downloadUri.toString());
Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
downloadUri.toString());
mDatabaseRef.push().setValue(upload);
} else {
Toast.makeText(ActivityUltimateMemeWorks.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
Now Let me explain why it wasn't working for you. If you see your code you assigned imageIdentifier
in onCreateView
means it will reload only when user start you're app not all the time when he try to upload the Image. So you need to reset the name of image when he click on button or call the Function. So just put it inside the function means when he click on Upload. Function will be called and value recalled and return new value.
Upvotes: 1