รายละเอียดของการตอบ ::
ตรงสีแดงอะครับ ผมไม่เข้าใจ
package com.example.admin.collarproject;
import android.app.DatePickerDialog;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.google.android.gms.tasks.Tasks;
import com.google.android.gms.tasks.Task;
import com.squareup.picasso.Picasso;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
public class page5 extends AppCompatActivity implements View.OnClickListener {
private Button buttonChoose;
private ImageView imageView2;
private Uri filePath;
public static final String FB_STORAGE_PATH = "image/";
public static final int REQUEST_CODE = 1234;
public static final int CAMERA_REQUEST_CODE = 1;
FirebaseStorage storage;
StorageReference storageReference;
private Spinner mySpinner;
private int day,month,year;
private EditText editTextCollarID;
private EditText editTextPetName;
private TextView editTextPetBrithDay;
private Button buttonSavePet;
private ProgressDialog progressDialog;
private FirebaseDatabase firebaseDatabase;
private FirebaseAuth firebaseAuth;
private StorageReference mStorageRef;
private DatabaseReference databaseReference;
DatePickerDialog datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page5);
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
mStorageRef = FirebaseStorage.getInstance().getReference();
databaseReference = FirebaseDatabase.getInstance().getReference();
progressDialog = new ProgressDialog(this);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
mySpinner = (Spinner) findViewById(R.id.gerderSpinner);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(page5.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.gender));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
buttonChoose = (Button) findViewById(R.id.buttonChoose);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setScaleType ( ImageView.ScaleType.CENTER_CROP );
editTextPetBrithDay = (TextView) findViewById(R.id.editTextPetBrithDay);
editTextCollarID = (EditText) findViewById(R.id.editTextCollarID);
editTextPetName = (EditText) findViewById(R.id.editTextPetName);
buttonSavePet = (Button) findViewById(R.id.buttonSavePet);
editTextPetBrithDay.setOnClickListener(this);
buttonSavePet.setOnClickListener(this);
buttonChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
}
public void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "เลือกรูปภาพ"), REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView2.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getImageExt(Uri uri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
@Override
public void onClick(View v) {
if (v == buttonSavePet) {
uploadImages();
}
if (v == editTextPetBrithDay) {
final Calendar c = Calendar.getInstance();
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH);
year = c.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
editTextPetBrithDay.setText(dayOfMonth+"-"+(monthOfYear+1)+"-"+year);
}
}
,day,month,year);
datePickerDialog.show();
}
}
private void uploadImages() {
if (filePath != null) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("กำลังลงทะเบียนสัตว์เลี้ยง");
dialog.show();
StorageReference ref = mStorageRef.child(FB_STORAGE_PATH + System.currentTimeMillis() + "." + getImageExt(filePath));
ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "ลงทะเบียนเสร็จสิ้น", Toast.LENGTH_SHORT).show();
taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
[glow=red] [font=Verdana] @Override
public void onSuccess(Uri uri) {
String collarID = editTextCollarID.getText().toString().trim();
String petName = editTextPetName.getText().toString().trim();
String petBrithDay = editTextPetBrithDay.getText().toString().trim();
String gender = mySpinner.getSelectedItem().toString();
ImageUpload imageUpload = new ImageUpload(uri.toString(), collarID, petName, petBrithDay, gender);
FirebaseUser user = firebaseAuth.getCurrentUser();
var id_increase = 0;
databaseReference.child(user.getUid()).child("PetProfile01").setValue(imageUpload);[/font]
[/glow]
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
dialog.setMessage("รอสักครู่ " + (int)progress +" %");
if (progress == 100) {
Intent p1 = new Intent(page5.this, page6.class);
startActivity(p1);
}
}
});
} else {
Toast.makeText(getApplicationContext(), "Please select image", Toast.LENGTH_SHORT).show();
}
}
}