2014-02-10 59 views

回答

7

該解決方案依賴於一個版本的AOSP庫2的包裝正在安裝在設備上。你可以這樣說:

// 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 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果凍豆。

我不確定這是否適用於所有Android設備。 更可靠的解決方案可能是分配Gallery2應用程序,並將TrimVideo活動及其依賴關係放入可與您的應用程序一起交付的庫中。 希望這有助於反正。

+0

在我的Nexus 5上工作,歡呼 – user1447209

+0

只希望它會在剪切後返回到我的應用程序,保存 – user1447209

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> = 2.2,但持續時間限制不會對API 2.1工作

+0

我不想拍視頻我說我已經可以做了,我想修剪一個已經存在的視頻 – user1447209