3

I am designing an app with media functionality, one of which is video capturing. I am using a default call to the Camera instead of creating my own camera. The problem I am having is that after capturing a video it does not save into a video file I designated, but this problem, so far that I have tested, only happens on Samsung phones using the stock camera app. It works fine if I use the Google Camera on a Samsung phone or a phone from a different company. Is this a known bug for Samsung phones or am I just doing something wrong? If it is is there a workaround it?

Here is my videoHandler.

private void videoHandler() {
    Intent intent = new Intent( MediaStore.ACTION_VIDEO_CAPTURE );
    if ( intent.resolveActivity( getPackageManager() ) != null ) {
        File videoFile = null;
        try {
            videoFile = createVideoFile();
        } catch ( IOException e ) {
            System.out.println( "Error creating file." );
        }

        if ( videoFile != null ) {
            videoUri = Uri.fromFile( videoFile );
            intent.putExtra( MediaStore.EXTRA_OUTPUT, videoUri );
            intent.putExtra( MediaStore.EXTRA_VIDEO_QUALITY, 1 );
            intent.putExtra( MediaStore.EXTRA_SIZE_LIMIT, 15000000L );
            intent.putExtra( MediaStore.EXTRA_DURATION_LIMIT, 6 );
            startActivityForResult( intent, VIDEO_REQUEST_CODE );
        }
    }
}

It calls createVideoFile, where I pre create a video file for a destination.

private File createVideoFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss" ).format( new Date() );
    String imageFileName = "MP4_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES );
    File video = File.createTempFile(
            imageFileName,  /* prefix */
            ".mp4",         /* suffix */
            storageDir      /* directory */
    );

    this.videoUri = Uri.fromFile( video );
    Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE );
    mediaScanIntent.setData( this.videoUri );
    this.sendBroadcast( mediaScanIntent );
    return video;
}

Here is the error I get when I try calling the videouri in my onActivityResult.

01-17 16:37:23.254  27162-28145/com.example.kevinem.blitz E/ImageLoader﹕ /storage/emulated/0/Pictures/MP4_20150117_163704_-918353230.mp4: open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException: /storage/emulated/0/Pictures/MP4_20150117_163704_-918353230.mp4: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:409)
        at java.io.FileInputStream.<init>(FileInputStream.java:78)
        at java.io.FileInputStream.<init>(FileInputStream.java:105)
        at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromFile(BaseImageDownloader.java:160)
        at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:88)
        at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:93)
        at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:73)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:264)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
        at libcore.io.Posix.open(Native Method)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
        at libcore.io.IoBridge.open(IoBridge.java:393)
danigh
  • 41
  • 1
  • 5

2 Answers2

1

I solved my problem. Basically I found someone saying this.

There is no requirement that a camera app follow the rules of ACTION_IMAGE_CAPTURE or ACTION_VIDEO_CAPTURE. Some will honor a specific requested output location. Some will not, and will return where they chose to store the image/video in the Uri contained in the result Intent delivered on onActivityResult(). Some may be completely broken and not tell you anything about where the image/video was stored, even if there was one stored.

Then I found in a different post that I had to manually retrieve it.

if (resultCode == -1) {
        //create a MediaScanner to save the vid into the indicate path
        new MediaScannerConnectionClient() {
            private MediaScannerConnection msc = null;
            {
                msc = new MediaScannerConnection(
                        getApplicationContext(), this);
                msc.connect();
            }

            public void onMediaScannerConnected() {
                msc.scanFile(name_vid, null);
            }

            public void onScanCompleted(String path, Uri uri) {
                msc.disconnect();

            }
        };
        Toast.makeText(this,"Video saved succesfull",
                Toast.LENGTH_SHORT).show();
}

name_vid is the path where you want to save the file in String format.

danigh
  • 41
  • 1
  • 5
0

Is there a particular reason you are using File.createTempFile( )?

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null).

The Files.createTempFile method provides an alternative method to create an empty file in the temporary-file directory. Files created by that method may have more restrictive access permissions to files created by this method and so may be more suited to security-sensitive applications.

Not sure if this is producing the intended result. I recommend taking look at this documentation.

http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)

particularly this piece.

File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
return file.exists();

tl;dr

I think this will fix yo shiz

 File video = new File(storageDir,  imageFileName + ".mp4")
progrenhard
  • 2,333
  • 2
  • 14
  • 14