Question

I'm using this code to record a video and save it to the sdcard.

MainActivity.class

public void onClickCaptImage() {
//...
        folder = new File(Environment.getExternalStorageDirectory()
                + "/neoadn/neoadnVideo/");
        if (!folder.exists()) {
            if (!folder.mkdirs()) {
                Log.d("UserFolder", "failed to create directory");
            }
        }
        videoName = "test" + ".mp4";
        file = new File(folder.getAbsolutePath() + "/" + videoName );

        mVideoUri = Uri.fromFile(file);

        //....

        Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mVideoUri);
        startActivityForResult(intent, RESULT_VIDEO_REALIZADO);
    }

AndroidManifest.xml

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

This code works correctly on most of the devices. The problem is that I have had in a Xperia Mini Pro (4.0.4) and do not save the video in the sdcard and does not give error.

The only difference I see is that when finished recording on the Xperia Mini Pro goes directly to the Main screen, in my nexus 4 shows the option to accept or cancel the recorded video.

When call-data.getData()- into onActivityResult() app returns the following:

05-11 20:16:04.310: I/data(4542): content://media/external/video/media/287

But there is no file.

Was it helpful?

Solution

Finally with this code i can obtain file. Pick the uri of the video and copy the video with the name you want to the folder

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("CODES", "" + requestCode + ", " + resultCode + ", " + data);
        if (requestCode == RESULT_VIDEO_REALIZADO && resultCode == RESULT_OK) {



            String filePath = null;
            Uri _uri = data.getData();
            Log.d("", "URI = " + _uri);
            if (_uri != null && "content".equals(_uri.getScheme())) {
                Cursor cursor = this
                        .getContentResolver()
                        .query(_uri,
                                new String[] { android.provider.MediaStore.Video.VideoColumns.DATA },
                                null, null, null);
                cursor.moveToFirst();
                filePath = cursor.getString(0);
                cursor.close();
            } else {
                filePath = _uri.getPath();
            }
            Log.d("", "Chosen path = " + filePath);
            if (!file.exists()) {
                copyFile(filePath, file.toString());
                Log.i("COPY", "Copy: " + videoUri.toString() + " a "
                        + file.toString());
            }
            Log.i("uriVid", videoUri.getPath());

    }


private void copyFile(String inputFile, String outputFile) {

    InputStream in = null;
    OutputStream out = null;
    try {

        // create output directory if it doesn't exist
        File dir = new File(folder.toString());
        if (!dir.exists()) {
            dir.mkdirs();
        }

        in = new FileInputStream(inputFile);
        out = new FileOutputStream(outputFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

        // write the output file (You have now copied the file)
        out.flush();
        out.close();
        out = null;

    } catch (FileNotFoundException fnfe1) {
        Log.e("tag", fnfe1.getMessage());
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

}

OTHER TIPS

It looks like the video recorder is saving the video to a default location. Perhaps this is because the mVideoUrl doesn't point where you expect it to be pointing. I think this might be because you're assuming the file system uses / as a folder separator.

Try changing:

file = new File(folder.getAbsolutePath() + "/" + videoName);

for:

file = new File(folder, videoName);

and check that file is capable of being written to by calling:

file.canWrite();

See [http://developer.android.com/reference/java/io/File.html#File(java.io.File,%20java.lang.String)]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top