0

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();
                }
            });
        }
    }
}
Hülya
  • 3,353
  • 2
  • 12
  • 19
  • Are you sure it's not `child(getIntent().getStringExtra("dato"))` that's throwing that Exception? It doesn't look like `fecha + " " + uri.getLastPathSegment()` can be null or empty, and the camera stuff all looks correct, at first glance. – Mike M. Jul 23 '20 at 08:45
  • @MikeM. yes, but "data" is arriving as null, so that line is not working for the same reason – Pedro A Galera Jul 23 '20 at 08:54
  • I'm not sure what you mean. If `data` were null, it would throw a `NullPointerException` at `data.getData()`, before it ever got to the Firebase stuff. – Mike M. Jul 23 '20 at 08:56
  • @MikeM.the error happened when i use URI or INTENT, so I suppose that "data" is the problem. I'm not sure really – Pedro A Galera Jul 23 '20 at 09:00
  • Have you checked to see what `getIntent().getStringExtra("dato")` is returning there? – Mike M. Jul 23 '20 at 09:03
  • @MikeM.sorry but I don´t know how do it, it´s my first time with Andoid Studio – Pedro A Galera Jul 23 '20 at 09:08
  • Well, you could [log it](https://developer.android.com/studio/debug/am-logcat#WriteLogs), then [check the logcat](https://developer.android.com/studio/debug/am-logcat#running). Or you could use [the debugger](https://developer.android.com/studio/debug), and set a breakpoint to check the value at runtime. The first one might be simpler to do, if you're still unfamiliar with the tools. – Mike M. Jul 23 '20 at 09:15
  • @MikeM.Caused by: java.lang.NullPointerException: println needs a message, so It's null – Pedro A Galera Jul 23 '20 at 09:39
  • 1
    Yeah, that's the immediate issue, then. `getIntent().getStringExtra("dato")` is returning null. Did you remember to put that `"dato"` extra on the `Intent` you used to start `TakePhoto`? – Mike M. Jul 23 '20 at 09:44
  • @MikeM.okay, I have fixed that error, but now it tells me that the getLastPathSegment() is null because uri is still null. – Pedro A Galera Jul 24 '20 at 08:05
  • Oh, yeah, I kinda forgot how that camera action works. I don't think you should be expecting to get a `Uri` back. The only thing that `ACTION_IMAGE_CAPTURE` is documented to return for your setup is a thumbnail `Bitmap` in the extras, which you can retrieve like is shown [in this snippet](https://developer.android.com/training/camera/photobasics#TaskPhotoView). Are you following some example somewhere? Are you sure they aren't creating the `Uri` elsewhere, and then attaching it to the `Intent` first, as an `EXTRA_OUTPUT`, maybe? – Mike M. Jul 24 '20 at 08:20
  • @MikeM.I used Uri because it's the only way i found to upload a photo to the Realtime Database with a random name and my "dato" text – Pedro A Galera Jul 24 '20 at 08:26
  • Right, but I'm wondering what led you to do `Uri uri = data.getData();` there. I mean, why were you thinking that `data.getData()` had your `Uri`? – Mike M. Jul 24 '20 at 08:32
  • @MikeM. for a tutorial I was following where they did it. As I said it is the first time that I work with this and some things I do not fully understand sorry – Pedro A Galera Jul 24 '20 at 08:34
  • No worries. I'm just trying to understand what the tutorial is doing, exactly. Do you have a link? – Mike M. Jul 24 '20 at 08:36
  • @MikeM.sorry I've seen so many tutorials that I don't even know what it is. Right now I just need to know how to upload the photo to the database in a folder with my name "dato". Sorry for the problems... – Pedro A Galera Jul 24 '20 at 08:39
  • OK, well, it's gonna take some work, unfortunately, if you need a full-size image. Have a look at [this answer](https://stackoverflow.com/a/45388858), and the project it links. If you get that all set up, then the `Uri outputUri` in the `onActivityResult()` method there is what you'd use for the Firebase upload. – Mike M. Jul 24 '20 at 08:46
  • 1
    @MikeM.okay i will try it. Thank you – Pedro A Galera Jul 24 '20 at 08:53

0 Answers0