我现在可以拍摄一个有意图的视频,创建一个启动默认视频剪辑活动的意图的细节是什么?并检查它是否出现在设备上?
答案 0 :(得分:8)
此解决方案依赖于设备上安装的AOSP Gallery2软件包版本。你可以这样做:
// The Intent action is not yet published as a constant in the Intent class
// This one is served by the com.android.gallery3d.app.TrimVideo activity
// which relies on having the Gallery2 app or a compatible derivative installed
Intent trimVideoIntent = new Intent("com.android.camera.action.TRIM");
// The key for the extra has been discovered from com.android.gallery3d.app.PhotoPage.KEY_MEDIA_ITEM_PATH
trimVideoIntent.putExtra("media-item-path", getFilePathFromVideoURI(this, videoUri));
trimVideoIntent.setData(videoUri);
// Check if the device can handle the Intent
List<ResolveInfo> list = getPackageManager().queryIntentActivities(trimVideoIntent, 0);
if (null != list && list.size() > 0) {
startActivity(trimVideoIntent); // Fires TrimVideo activity into being active
}
方法getFilePathFromVideURI
基于此问题的答案:Get filename and path from URI from mediastore
public String getFilePathFromVideoURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Video.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
videoUri
是一个Uri
指向这样的内容:content://media/external/video/media/43
。您可以通过发出ACTION_PICK Intent:
Intent pickVideoUriIntent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoUriIntent, PICK_VIDEO_REQUEST);
在onActivityResult
中得到这样的uri:
....
case PICK_VIDEO_REQUEST:
Uri videoUri = data.getData();
...
此解决方案适用于我的Galaxy Nexus和Android 4.3 Jelly Bean。
我不确定这是否适用于所有Android设备。 更可靠的解决方案可能是分叉Gallery2应用程序并将TrimVideo活动及其依赖项放入可随应用程序提供的库中。 希望这无论如何都有帮助。
答案 1 :(得分:0)
尝试这可能有帮助
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra("android.intent.extra.durationLimit", 30000);
intent.putExtra("EXTRA_VIDEO_QUALITY", 0);
startActivityForResult(intent, ActivityRequests.REQUEST_TAKE_VIDEO);
此代码适用于API&gt; = 2.2,但持续时间限制不适用于API 2.1