英文:
childName cannot be null or empty with camera in Android Studio
问题
当我运行它时,我收到一个错误,提示 childName
不能为空。变量 "data" 在拍照时变为 null,我不知道如何修复它。
这是我的代码。
public class TakePhoto extends AppCompatActivity {
private Button btnPhoto;
private ImageView mImageView;
private static final int CAMERA_REQUEST_CODE = 1;
private StorageReference storage;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String fecha = df.format(c.getTime());
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
storage = FirebaseStorage.getInstance().getReference();
btnPhoto = (Button) findViewById(R.id.buttonCamera2);
mImageView = (ImageView) findViewById(R.id.fotoFinal);
progress = new ProgressDialog(this);
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
Uri uri = data.getData();
StorageReference filepath = storage.child(getIntent().getStringExtra("dato")).child(fecha + " " + uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(TakePhoto.this, "Subiendo...", Toast.LENGTH_LONG).show();
}
});
}
}
}
英文:
When I run it, I get an error saying that childName
cannot be null. The variable "data" is getting null when taking the photo and I don't know how to fix it.
This is my code.
public class TakePhoto extends AppCompatActivity {
private Button btnPhoto;
private ImageView mImageView;
private static final int CAMERA_REQUEST_CODE = 1;
private StorageReference storage;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String fecha = df.format(c.getTime());
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
storage = FirebaseStorage.getInstance().getReference();
btnPhoto = (Button) findViewById(R.id.buttonCamera2);
mImageView = (ImageView) findViewById(R.id.fotoFinal);
progress = new ProgressDialog(this);
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
Uri uri = data.getData();
StorageReference filepath = storage.child(getIntent().getStringExtra("dato")).child(fecha + " " + uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(TakePhoto.this, "Subiendo...", Toast.LENGTH_LONG).show();
}
});
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论