我希望了解手机上的视频,选择一个并在我的应用中查看。我差不多......最后一点。当我点击视频时,我会回到我的应用程序,但视频没有显示。我错过了什么?
private static int RESULT_LOAD_VIDEO = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_VIDEO);
/* Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_VIDEO);*/
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK && null != data) {
Uri selectedVideo = data.getData();
String[] filePathColumn = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(selectedVideo,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
VideoView imageView = (VideoView) findViewById(R.id.video1);
imageView.setVideoPath(picturePath);
}
}
答案 0 :(得分:0)
我认为您至少需要调用imageView.start()来播放视频(假设您已正确设置其他所有内容)。
您还可以调用已安装的Android视频播放器,只需几行代码即可播放视频。
Uri fileUri = Uri.fromFile(videoFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri,
URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
Intent选择器将出现让用户选择他喜欢的视频播放器。我写了一篇博客来讨论如何详细地做到这一点http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player。希望它有所帮助。