0

I am using intent camera to record videos. I have tested my app on Samsung, HTC, LG & Motorola Devices it works smoothly but on Nexus Phones its crashing after video is recorded and the file is selected for compression here is the code I am using to start intent and getting the video recorded file.

 case R.id.camera_icon:
                Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                uri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
                if (uri == null) {
                    // display an error
                    Toast.makeText(MainActivity.this, R.string.error_external_storage,
                            Toast.LENGTH_LONG).show();
                } else {
                    videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
                    videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                    // 0 = lowest res

                    startActivityForResult(videoIntent, RESULT_CODE_COMPRESS_VIDEO);

                }

Creating Storage Location:

private Uri getOutputMediaFileUri(int mediaType) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.
        if (isExternalStorageAvailable()) {
            // get the URI

            // 1. Get the external storage directory
            String appName = MainActivity.this.getString(R.string.app_name);
            File mediaStorageDir = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    appName);

            // 2. Create our subdirectory
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.e(TAG, "Failed to create directory.");
                    return null;
                }
            }

            String path = mediaStorageDir.getPath() + File.separator;
            if (mediaType == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(path + "thumb" + ".jpg");
            } else if (mediaType == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(path + "video" + ".mp4");
            } else {
                return null;
            }

            Log.d(TAG, "File: " + Uri.fromFile(mediaFile));

            // 5. Return the file's URI
            return Uri.fromFile(mediaFile);

        } else {
            return null;
        }
    }
Contextioner
  • 75
  • 11
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jun 04 '16 at 23:45
  • @CommonsWare actually the nexus device is not mine I tested it on a friends phone. That's why I don't have the log cat – Contextioner Jun 04 '16 at 23:59
  • Then borrow the friend's phone again, run your test, and examine LogCat. For all you and I know, the problem is nowhere in this code, but rather is in your `onActivityResult()` method, or somewhere else. The only thing I see in this code that might work on those other devices but not a Nexus is your use of external storage. If your `targetSdkVersion` is 23 or higher, and the Nexus in question was running Android 6.0 or higher, [make sure that you are handling the `WRITE_EXTERNAL_STORAGE` permission properly](http://stackoverflow.com/a/32636039/115145). – CommonsWare Jun 05 '16 at 00:05
  • @CommonsWare all the other devices I have used are 6.0 and all have the permission for WRITE_EXTERNAL_STORAGE. Its working properly on the other devices except than Nexus phones. or could it be because of getExternalPublicStorageDirectory? – Contextioner Jun 05 '16 at 00:12
  • Again: borrow the friend's phone again, run your test, and examine LogCat. – CommonsWare Jun 05 '16 at 10:47

0 Answers0