4

我以编程方式打开相机拍摄视频。我告诉相机使用如下代码将视频文件放到指定位置:

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File out = new File("/sdcard/camera.mp4");
Uri uri = Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, GlobalUtility.CAMERA_VIDEO);

它在 HTC 手机上运行良好。但在我的 moto 中,它只是忽略 MediaStore.EXTRA_OUTPUT 参数,并将视频放到默认位置。那么我在 onActivityResult() 函数中使用这段代码来解决问题:

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

String realPath;
try {
    File file = new File("/sdcard/camera.mp4");
    if (!file.exists()) {
        Uri videoUri = data.getData();
        realPath = getRealPathFromURI(videoUri);
    }
} catch (Exception ex) {
    Uri videoUri = data.getData();
    realPath = getRealPathFromURI(videoUri);
}

希望这会对其他人有所帮助。

4

2 回答 2

0

I have done this way and still didn't found any error..so please try this in you "moto defy" so I can know the reality.

To Call Intent :

Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,2323);

In Activity on Result:

Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);

videoView.setVideoPath(path);
于 2011-11-10T13:46:46.897 回答
0

仅仅因为/sdcard/一部手机上的 sdcard 目录和一个 Android 版本并不意味着它会保持一致。

您将希望Environment.getExternalStorageDirectory()按照弗兰肯斯坦的评论建议使用。这将始终用于获取 SD 卡的目录。

您还需要检查 SD 卡当前是否可安装,因为手机可能处于 USB 存储模式。

尝试类似...

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    Log.d(TAG, "No SDCARD");
} else {
    File out = new File(Environment.getExternalStorageDirectory()+File.separator+"camera.mp4");     
}
于 2011-11-10T02:06:00.107 回答