package com.example.game.chatshat;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import de.hdodenhof.circleimageview.CircleImageView;
import id.zelory.compressor.Compressor;
public class SettingsActivity extends AppCompatActivity {
private CircleImageView setting_image;
private TextView setting_name,setting_status;
private Button setting_changepic,setting_update_status;
private DatabaseReference retrieve;
private FirebaseAuth auth;
private final static int Gallery_Pick=1;
private StorageReference profile_image_store;
private Bitmap thumb_bitmap=null;
private StorageReference thumb_image_ref;
private ProgressDialog loadingbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
auth=FirebaseAuth.getInstance();
profile_image_store= FirebaseStorage.getInstance().getReference().child("" +
"Profile_Pics");
String Online_user=auth.getCurrentUser().getUid();
retrieve= FirebaseDatabase.getInstance().getReference().child("Users").child(Online_user);
thumb_image_ref=FirebaseStorage.getInstance().getReference().child("thumb_pics");
setting_image=(CircleImageView) findViewById(R.id.circularImage);
setting_name=(TextView) findViewById(R.id.textView5);
setting_status=(TextView) findViewById(R.id.textView8);
setting_update_status=(Button) findViewById(R.id.button7);
setting_changepic=(Button) findViewById(R.id.button6);
loadingbar=new ProgressDialog(this);
setting_update_status.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent statusIntent=new Intent(SettingsActivity.this,Change_Status.class);
startActivity(statusIntent);
}
});
retrieve.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String user_name = dataSnapshot.child("user_name").getValue().toString();
String user_status = dataSnapshot.child("user_Status").getValue().toString();
String user_image = dataSnapshot.child("user_pics").getValue().toString();
String user_thumb = dataSnapshot.child("user_thumb_pic").getValue().toString();
setting_name.setText(user_name);
setting_status.setText(user_status);
if (!user_image.equals("profile"))
{
Picasso.with(SettingsActivity.this).load(user_image).placeholder(R.drawable.profile).into(setting_image);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
setting_changepic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent=new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,Gallery_Pick);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
{
Uri Imageuri=data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK)
{ loadingbar.setTitle("uploading Your Profile Pics" );
loadingbar.setMessage("Please wait");
loadingbar.show();
Uri resultUri = result.getUri();
File thumb_file_path_uri=new File(resultUri.getPath());
String Online_user=auth.getCurrentUser().getUid();
try
{
thumb_bitmap=new Compressor(this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(50)
.compressToBitmap( thumb_file_path_uri);
}
catch(IOException e)
{
e.printStackTrace();
}
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);
final byte[] thumb_byte=byteArrayOutputStream.toByteArray();
StorageReference filepath=profile_image_store.child(Online_user + ".jpg");
final StorageReference thumb_file_path=thumb_image_ref.child(Online_user+".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
{
if(task.isSuccessful())
{
Toast.makeText(SettingsActivity.this,"Uploading your Profile Pic", Toast.LENGTH_LONG).show();
final String download_url=task.getResult().getDownloadUrl().toString();
UploadTask uploadTask=thumb_file_path.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task)
{
String thumb_download_url=thumb_task.getResult().getDownloadUrl().toString();
if(task.isSuccessful())
{
Map update_user_data=new HashMap();
update_user_data.put("user_pics",download_url);
update_user_data.put("user_thumb_pic",thumb_download_url);
retrieve.updateChildren(update_user_data)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(SettingsActivity.this, "Image uploaded success", Toast.LENGTH_SHORT).show();
loadingbar.dismiss();
}
});
}
}
});
}
else
{
Toast.makeText(SettingsActivity.this, "Error", Toast.LENGTH_SHORT).show();
loadingbar.dismiss();
}
}
});
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
whenever i trying to update my profile pic the app suddenly stopped help me out ...the issue starts when i go to account settings and trying to upload new pic .i am trying to compress the image during uploading and trying to store it in the firebase storge.
Please help me to solve this error.