如何从图库SD卡中选择一个视频文件并在App视图中播放?

时间:2014-05-05 07:22:55

标签: android video

我从画廊中选择一个视频然后播放它但是我无法播放它但是对于图像我得到它就像我也尝试过视频但是它出错了如果有人有任何想法,请帮助我们。

这是我的代码,但它不起作用,

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;

public class videoView extends Activity implements OnClickListener {

     private VideoView videoPreview;
Button button;
private static final int SELECT_VIDEO_REQUEST = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview);

        button  = (Button) findViewById(R.id.button1);
        button.setOnClickListener((OnClickListener) this);
}
    public void onClick(View v) {
        if(v ==button){

            Intent intent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, SELECT_VIDEO_REQUEST);

    }}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Video.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex); 
        cursor.close();

         videoPreview = (VideoView) findViewById(R.id.videoPreview);
         videoPreview.setVideoPath(picturePath);
}
}}

1 个答案:

答案 0 :(得分:1)

这是一个简单的代码,用于通过意图打开图库然后在这个方法中查看它请使用它对你有帮助

public class videoView extends Activity implements OnClickListener {

        Button gallybutton;
           private Cursor videocursor;
              private int video_column_index;
              ListView videolist;
              int count;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.videoview);
            gallybutton  = (Button) findViewById(R.id.gally);
            gallybutton.setOnClickListener((OnClickListener) this);

        }
            public void onClick(View v) {


          init_phone_video_grid();
    }
      public void init_phone_video_grid() {
              System.gc();
              String[] proj = { MediaStore.Video.Media._ID,
    MediaStore.Video.Media.DATA,
    MediaStore.Video.Media.DISPLAY_NAME,
    MediaStore.Video.Media.SIZE };
              videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,   proj, null, null, null);
              count = videocursor.getCount();
              videolist = (ListView) findViewById(R.id.PhoneVideoList);
              videolist.setAdapter(new VideoAdapter(getApplicationContext()));
              videolist.setOnItemClickListener(videogridlistener);
        }

        public OnItemClickListener videogridlistener = new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View v, int position,
    long id) {
                    System.gc();
                    video_column_index = videocursor
    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                    videocursor.moveToPosition(position);
                    String filename = videocursor.getString(video_column_index); 
                    Intent intent = new Intent(videoView.this, gallyVideo.class);
                    intent.putExtra("videofilename", filename);
                    startActivity(intent);
              }
        };

        public class VideoAdapter extends BaseAdapter {
              private Context vContext;

              public VideoAdapter(Context c) {
                    vContext = c;
              }

              public int getCount() {
                    return count;
              }

              public Object getItem(int position) {
                    return position;
              }

              public long getItemId(int position) {
                    return position;
              }

              public View getView(int position, View convertView, ViewGroup parent) {
                    System.gc();
                    TextView tv = new TextView(vContext.getApplicationContext());
                    String id = null;
                    if (convertView == null) {
                          video_column_index = videocursor
    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                          videocursor.moveToPosition(position);
                          id = videocursor.getString(video_column_index);
                          video_column_index = videocursor
    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
                          videocursor.moveToPosition(position);
                          id += " Size(KB):" + videocursor.getString(video_column_index);
                          tv.setText(id);
                    } else
                          tv = (TextView) convertView;
                    return tv;
              }
        }

}