Start the trim video activity with an intent

前端 未结 2 1278
长发绾君心
长发绾君心 2020-12-17 02:54

I can take a video with an intent now what are the details to create an intent to start the default video trimmer activity? And check if it present on the device?

相关标签:
2条回答
  • 2020-12-17 03:11

    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.

    0 讨论(0)
  • 2020-12-17 03:20

    Try this may it helps

    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);
    

    This code works well on API >=2.2, but the duration limit does not work on API 2.1

    0 讨论(0)
提交回复
热议问题