use of com.google.firebase.firestore.DocumentReference in project PhotoBlog-Android-Blog-App by akshayejh.
the class NewPostActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_post);
storageReference = FirebaseStorage.getInstance().getReference();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
current_user_id = firebaseAuth.getCurrentUser().getUid();
newPostToolbar = findViewById(R.id.new_post_toolbar);
setSupportActionBar(newPostToolbar);
getSupportActionBar().setTitle("Add New Post");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
newPostImage = findViewById(R.id.new_post_image);
newPostDesc = findViewById(R.id.new_post_desc);
newPostBtn = findViewById(R.id.post_btn);
newPostProgress = findViewById(R.id.new_post_progress);
newPostImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).setMinCropResultSize(512, 512).setAspectRatio(1, 1).start(NewPostActivity.this);
}
});
newPostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String desc = newPostDesc.getText().toString();
if (!TextUtils.isEmpty(desc) && postImageUri != null) {
newPostProgress.setVisibility(View.VISIBLE);
final String randomName = UUID.randomUUID().toString();
// PHOTO UPLOAD
File newImageFile = new File(postImageUri.getPath());
try {
compressedImageFile = new Compressor(NewPostActivity.this).setMaxHeight(720).setMaxWidth(720).setQuality(50).compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();
// PHOTO UPLOAD
UploadTask filePath = storageReference.child("post_images").child(randomName + ".jpg").putBytes(imageData);
filePath.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task) {
final String downloadUri = task.getResult().getDownloadUrl().toString();
if (task.isSuccessful()) {
File newThumbFile = new File(postImageUri.getPath());
try {
compressedImageFile = new Compressor(NewPostActivity.this).setMaxHeight(100).setMaxWidth(100).setQuality(1).compressToBitmap(newThumbFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();
UploadTask uploadTask = storageReference.child("post_images/thumbs").child(randomName + ".jpg").putBytes(thumbData);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String downloadthumbUri = taskSnapshot.getDownloadUrl().toString();
Map<String, Object> postMap = new HashMap<>();
postMap.put("image_url", downloadUri);
postMap.put("image_thumb", downloadthumbUri);
postMap.put("desc", desc);
postMap.put("user_id", current_user_id);
postMap.put("timestamp", FieldValue.serverTimestamp());
firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()) {
Toast.makeText(NewPostActivity.this, "Post was added", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(NewPostActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
}
newPostProgress.setVisibility(View.INVISIBLE);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error handling
}
});
} else {
newPostProgress.setVisibility(View.INVISIBLE);
}
}
});
}
}
});
}
Aggregations