Android:来自图库的视频选择器

时间:2014-06-02 12:30:37

标签: android

我想从图库中选择视频。我曾使用意图打开图库但不知道应该在视频的活动结果上做些什么。我可以选择想要获取视频路径和视频缩略图的图像。

1 个答案:

答案 0 :(得分:10)

尝试使用以下代码:

public class GalleryVideoPickerActivity extends Activity {
    private static final int SELECT_VIDEO = 1;

    private String selectedVideoPath;

    @ Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECT_VIDEO); 
    }

    @ Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_VIDEO) {
                selectedVideoPath = getPath(data.getData());
                try {
                    if(selectedVideoPath == null) {
                        Log.e("selected video path = null!");
                        finish();
                    } else {
                            /**
                             * try to do something there
                             * selectedVideoPath is path to the selected video
                             */
                    }
                } catch (IOException e) {
                    //#debug
                    e.printStackTrace();
                }
            }
        }
        finish();
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }
}