小智 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
}
Run Code Online (Sandbox Code Playgroud)
该方法getFilePathFromVideURI基于以下问题的答案:从mediastore获取URI的文件名和路径
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
videoUri是Uri指着这样的事情:content://media/external/video/media/43.您可以通过发出ACTION_PICK意图来收集一个:
Intent pickVideoUriIntent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoUriIntent, PICK_VIDEO_REQUEST);
Run Code Online (Sandbox Code Playgroud)
在onActivityResult得到像这样的URI:
....
case PICK_VIDEO_REQUEST:
Uri videoUri = data.getData();
...
Run Code Online (Sandbox Code Playgroud)
此解决方案适用于我的Galaxy Nexus和Android 4.3 Jelly Bean.
我不确定这是否适用于所有Android设备.更可靠的解决方案可能是分叉Gallery2应用程序并将TrimVideo活动及其依赖项放入可随应用程序提供的库中.希望无论如何都有帮助.
| 归档时间: |
|
| 查看次数: |
3629 次 |
| 最近记录: |