Search in sources :

Example 1 with DocumentReference

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);
                        }
                    }
                });
            }
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) UploadTask(com.google.firebase.storage.UploadTask) HashMap(java.util.HashMap) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) DocumentReference(com.google.firebase.firestore.DocumentReference) Compressor(id.zelory.compressor.Compressor) Intent(android.content.Intent) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageView(android.widget.ImageView) CropImageView(com.theartofdev.edmodo.cropper.CropImageView) View(android.view.View) IOException(java.io.IOException) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) File(java.io.File) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)1 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1 Task (com.google.android.gms.tasks.Task)1 DocumentReference (com.google.firebase.firestore.DocumentReference)1 UploadTask (com.google.firebase.storage.UploadTask)1 CropImageView (com.theartofdev.edmodo.cropper.CropImageView)1 Compressor (id.zelory.compressor.Compressor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1