【问题标题】:how to pick a video file from gallery SDcard and play it in App view?如何从图库 SD 卡中选择视频文件并在 App 视图中播放?
【发布时间】:2014-06-12 15:00:29
【问题描述】:

我必须从图库中选择一个视频并播放它,但我无法播放它,但对于图像,我得到了它,就像我也尝试过视频一样,但如果有人有任何想法,它会出错帮帮我。

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

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

【问题讨论】:

  • 但此代码在 android 目标版本 14 以下运行。
  • 我认为它适用于所有人,请检查您的版本详细信息并进行更改。
  • 好的,让我再检查一次,如果我错了,请告诉我。
  • 但是先生,它显示列表视图我需要的很简单,只需打开图库并选择一个视频然后播放它。

标签: android video


【解决方案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;
              }
        }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多