This solution relies on a version of the AOSP Gallery2 package being installed on the device. You can do it like this:
// 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
}
The method getFilePathFromVideURI
is based on the answer of this question: 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
is an Uri
pointing to something like this: content://media/external/video/media/43
. You can gather one by issuing an ACTION_PICK Intent:
Intent pickVideoUriIntent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoUriIntent, PICK_VIDEO_REQUEST);
In onActivityResult
get the uri like so:
....
case PICK_VIDEO_REQUEST:
Uri videoUri = data.getData();
...
This solution works on my Galaxy Nexus with Android 4.3 Jelly Bean.
I am not sure if this is available on all Android devices.
A more reliable solution may be to fork the Gallery2 app and put the TrimVideo activity together with its dependencies into a library that can be delivered with your app.
Hope this helps anyway.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…