Question

i've to pick a video from gallery and play it but i can't play it but for image i got it as like that i'ad tried for video also but it goes some where wrong if some have any idea please help me guys.

here is my code but it is not working,

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);
}
}}
Was it helpful?

Solution

Here is a simple code which is used to open the gallery by intent then view it in this methos please use it helpful to you

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top