0

I'm trying to make test to my Android application on Android 4,5,6 and 7 and the action of open camera to record video is working very good on Android 4 and 5 but it's not working on android 6 and 7 and this error appears then the application is stopped, I don't know if we have any new thing to open camera and record video in Android 6 and 7

error:

java.lang.NullPointerException: file

Code :

private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_VIDEO = 2;
private Uri fileUri;

private void recordVideo() {
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
    // set video quality
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
    // name
    // start the video capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
   if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            // video successfully recorded
            // launching upload activity
            launchUploadActivity(false);

        } else if (resultCode == RESULT_CANCELED) {

            // user cancelled recording
            Toast.makeText(getApplicationContext(),
                    "User cancelled video recording", Toast.LENGTH_SHORT)
                    .show();

        } else {
            // failed to record video
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                    .show();
        }


    }
 }


public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}


 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_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}
 public String getPath(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getContentResolver().query(
            android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
    cursor.close();

    return path;
}

Permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Samah Ahmed
  • 419
  • 8
  • 24
  • "is working very good on Android 4 and 5 but it's not working on android 6 and 7" -- whether things work or not with `ACTION_VIDEO_CAPTURE` is determined more by the camera app as it is by the OS version. Beyond that, what *exactly* is `fileUri`? – CommonsWare Jan 07 '17 at 13:35
  • @CommonsWare I updated post with function , the error appears when trying to start activity to open Camera – Samah Ahmed Jan 07 '17 at 13:39
  • What exactly is `getOutputMediaFile(type)` returning? – CommonsWare Jan 07 '17 at 13:40
  • @CommonsWare I'm sorry again , the post is updated with full functions – Samah Ahmed Jan 07 '17 at 13:42
  • Is the exception coming from your code? In other words, does the stack trace refer to lines from your app, or does it refer to lines from the camera app? If it refers to lines from your app, post the full stack trace, and indicate what lines in the stack trace correspond to lines in your code. Also note that your `getPath()` method reflects anti-patterns. Do not assume a `Uri` has any particular structure, such as guessing that it has a colon in it or guessing that the `MediaStore` knows about it. – CommonsWare Jan 07 '17 at 13:47
  • I said the error is appear in the line of start intent of open camera and this error is only appear when I'm trying to test app on Android device with version 6 and 7 but it's working on another versions. – Samah Ahmed Jan 07 '17 at 13:49
  • have you requested permissions on runtime? (read and write) – Siarhei Jan 07 '17 at 14:01
  • @user5599807 I updated post with all permissions that is used in app – Samah Ahmed Jan 07 '17 at 14:02
  • means you need to ask permissions at runtime for 6+ http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission – Siarhei Jan 07 '17 at 14:05
  • "I said the error is appear in the line of start intent of open camera" -- that does not answer my question. – CommonsWare Jan 07 '17 at 14:13
  • @user5599807 I think you are true I'm reading now about OnRequestPermissions – Samah Ahmed Jan 07 '17 at 14:16

0 Answers0