0

this method keeps returning Null for some reason

it should create and return either a .jpg or a .mp4 file

private static File getOutputMediaFile(int type) {
    // External sdcard location
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            Config.IMAGE_DIRECTORY_NAME);
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(TAG, "Oops! Failed create "
                    + Config.IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }
    return mediaFile;
}

i can't figure out why? i call for it here in order to obtain a file Uri ontop of the specified action above

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = FileProvider.getUriForFile(Camera.this,
            "com.company.example.provider",
            Objects.requireNonNull(getOutputMediaFile(MEDIA_TYPE_IMAGE)));
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

but as soon as captureImage() gets to FileProvider.getUriForFile() it crashes due to Null Pointer Exception

2020-01-16 14:39:52.766 13603-13603/com.company.example E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.example, PID: 13603
    java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203)
        at com.company.example.Camera.captureImage(Camera.java:76)
        at com.company.example.Camera.lambda$onCreate$0$Camera(Camera.java:44)
        at com.company.example.-$$Lambda$Camera$TibLnS7dWo_dEUw9GC45GOADOCo.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:6274)
        at android.view.View$PerformClick.run(View.java:24859)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

2 Answers2

3

You should be able to find out what is actually happening by setting a breakpoint at the start of the method and single-stepping it to watch what it does.

Based on the code that you included, I can see two possible reasons.

  1. At the start of the method, it checks to see if a directory with a given name exists and attempts to create it if it doesn't exist by calling Files.mkdirs(). If the mkdirs call fails to create the directory, the method returns null.

  2. You are calling the method with MEDIA_TYPE_IMAGE as the type parameter, and that is apparently one of the supported types. However, we cannot see where MEDIA_TYPE_IMAGE is defined (or imported) so it is possible that there are two different definitions ... with different values. If they were different, then null could be returned.

It is also possible that the code that you are running doesn't match the code that you have shown us.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Upon replying to your original post I dig up this article from stackoverflow : MEDIA_TYPE_IMAGE not recognized

Your problem may be related to MEDIA-TYPE_IMAGE and/or MEDIA_TYPE_VIDEO not being recognized or found. Two options provided by the answers in that post include either referencing the Android provider containing this integer, or add it manually.

Try both of the options provided by the contributors and see if they resolve your problem.

Raekh Void
  • 441
  • 3
  • 11