Guest User

Untitled

a guest
Dec 8th, 2019
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package project.roei.com.mycloset;
  2.  
  3. import android.app.Activity;
  4. import android.app.DownloadManager;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.drawable.BitmapDrawable;
  11. import android.graphics.drawable.Drawable;
  12. import android.media.Image;
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import android.os.Environment;
  16. import android.os.SystemClock;
  17. import android.view.View;
  18. import android.view.textclassifier.TextClassification;
  19. import android.widget.Button;
  20. import android.widget.ImageView;
  21. import android.widget.Toast;
  22.  
  23. import com.google.android.gms.common.api.Response;
  24. import com.google.android.gms.tasks.OnFailureListener;
  25. import com.google.android.gms.tasks.OnSuccessListener;
  26. import com.google.firebase.database.DatabaseReference;
  27. import com.google.firebase.database.FirebaseDatabase;
  28. import com.google.firebase.storage.FirebaseStorage;
  29. import com.google.firebase.storage.StorageReference;
  30. import com.google.firebase.storage.UploadTask;
  31.  
  32. import java.io.ByteArrayOutputStream;
  33. import java.io.File;
  34. import java.io.FileOutputStream;
  35. import java.text.DateFormat;
  36. import java.text.SimpleDateFormat;
  37. import java.util.Calendar;
  38. import java.util.Date;
  39.  
  40. import androidx.annotation.NonNull;
  41.  
  42. import static project.roei.com.mycloset.MyClosetActivity.imageBitmap;
  43. import static project.roei.com.mycloset.MyClosetActivity.picturePath;
  44. import static project.roei.com.mycloset.UpdateActivity.EMAIL;
  45. import static project.roei.com.mycloset.UpdateActivity.NICKNAME;
  46. import static project.roei.com.mycloset.UpdateActivity.PREFS_NAME;
  47.  
  48. public class CheckImage extends Activity implements View.OnClickListener {
  49.  
  50. private Button btnCancel;
  51. private Button btnOk;
  52. public ImageView ivPhoto;
  53. SharedPreferences prefs;
  54.  
  55.  
  56. //TODO connect the storage and auth and databse using firebase.
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. Intent intent = getIntent();
  60. super.onCreate(savedInstanceState);
  61. setContentView(R.layout.activity_check_image);
  62.  
  63.  
  64. prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
  65.  
  66.  
  67.  
  68. btnCancel = (Button) findViewById(R.id.btnCancel);
  69. btnOk = (Button) findViewById(R.id.btnOk);
  70. ivPhoto = (ImageView) findViewById(R.id.ivPhoto);
  71.  
  72. btnCancel.setOnClickListener(this);
  73. btnOk.setOnClickListener(this);
  74.  
  75. String image = getIntent().getStringExtra("ImageBitmap");
  76. Bundle extra = getIntent().getExtras();
  77. if (imageBitmap != null){
  78. ivPhoto.setImageBitmap(imageBitmap);
  79. }
  80. if (picturePath != null){
  81. ivPhoto.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  82. }
  83.  
  84. }
  85.  
  86. @Override
  87. public void onBackPressed() {
  88. Intent i = new Intent(CheckImage.this, MyClosetActivity.class);
  89. startActivity(i);
  90. overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
  91. }
  92.  
  93.  
  94. @Override
  95. public void onClick(View view) {
  96. switch (view.getId()){
  97. case R.id.btnCancel:
  98. Intent i = new Intent(CheckImage.this, MyClosetActivity.class);
  99. startActivity(i);
  100. overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
  101. break;
  102. case R.id.btnOk:
  103. //Toast.makeText(this, ivPhoto.getDrawable().toString(), Toast.LENGTH_SHORT).show();
  104. FirebaseStorage Storage = FirebaseStorage.getInstance();
  105. StorageReference storageRef = Storage.getReference();
  106. prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
  107.  
  108. final Date formattedDate = Calendar.getInstance().getTime();
  109. final StorageReference Ref = storageRef.child(HomeActivity.User.getUid()).child("images").child(formattedDate.toString());
  110.  
  111. // Get the data from an ImageView as bytes
  112. ivPhoto.setDrawingCacheEnabled(true);
  113. ivPhoto.buildDrawingCache();
  114. Bitmap bitmap = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap();
  115. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  116. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  117. byte[] data = baos.toByteArray();
  118.  
  119. final UploadTask uploadTask = Ref.putBytes(data);
  120. uploadTask.addOnFailureListener(new OnFailureListener() {
  121. @Override
  122. public void onFailure(@NonNull Exception exception) {
  123. // Handle unsuccessful uploads
  124. Toast.makeText(CheckImage.this, "Failed to upload image - Contact Developer.", Toast.LENGTH_SHORT).show();
  125. Intent i = new Intent(CheckImage.this, MyClosetActivity.class);
  126. startActivity(i);
  127. overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
  128. }
  129. }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  130. @Override
  131. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  132. // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
  133. FirebaseDatabase database = FirebaseDatabase.getInstance();
  134. DatabaseReference RefUser = database.getReference("Users").child(HomeActivity.User.getUid()).child("Images").child(formattedDate.toString());
  135. String location = Ref.getPath();
  136. RefUser.setValue(location);
  137. Toast.makeText(CheckImage.this, "Image saved successfully", Toast.LENGTH_SHORT).show();
  138. Intent i = new Intent(CheckImage.this, MyClosetActivity.class);
  139. startActivity(i);
  140. overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
  141. // ...
  142. }
  143. });
  144. break;
  145. }
  146. }
  147. }
RAW Paste Data