• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Can't Play Video in Gallery

Hi,
Below is my code to access the video gallery but the application returns to it homepage everytime I choose a video/media file to play from the gallery. Kindly help.

Code:
package org;

import org.cehrd.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class cehrdActivity extends Activity {
    /** Called when the activity is first created. */

    //YOU CAN EDIT THIS TO WHATEVER YOU WANT
    private static final int SELECT_VIDEO = 1;

    private String selectedVideoPath;

    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button) findViewById(R.id.Button01))
        .setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                // in onCreate or any event where your want the user to
                // select a file
                Intent intent = new Intent();
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Select Video"), SELECT_VIDEO);
                       }
        });
        
        ((Button) findViewById(R.id.Button02))
        .setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                setContentView(R.layout.viewdownloaddir);
                WebView myWebView = (WebView) findViewById(R.id.webview);
                WebSettings webSettings = myWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                myWebView.loadUrl("http://10.42.43.1/senchalearn-teagrams/index.html");
            }
        });
    }

    //UPDATED
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_VIDEO) {
                //MEDIA GALLERY
                            
                 Uri selectedVideoUri = data.getData();
                 
                 //selectedVideoPath = getPath(selectedVideoUri);
                //selectedVideoUri.getPath();
                // Uri myUri = Uri.parse(selectedVideoPath); // initialize Uri here
                 
                Uri myUri = selectedVideoUri; // initialize Uri here
                //get current window information, and set format, set it up differently, if you need some special effects
                getWindow().setFormat(PixelFormat.TRANSLUCENT);
                //the VideoView will hold the video
                VideoView videoHolder = new VideoView(this);
                //MediaController is the ui control howering above the video (just like in the default youtube player).
                videoHolder.setMediaController(new MediaController(this));
                //assing a video file to the video holder
                videoHolder.setVideoURI(myUri);
                //get focus, before playing the video.
                videoHolder.requestFocus();
                videoHolder.start();
                
                

                 }
              }
           }
    
    //UPDATED!
    public String getPath(Uri uri) {
        String selectedVideoPath;
        //1:MEDIA GALLERY --- query from MediaStore.Video.Media.DATA
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor != null){
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            selectedVideoPath = cursor.getString(column_index);
        }else{
            selectedVideoPath = null;
        }

        if(selectedVideoPath == null){
            //2:OI FILE Manager --- call method: uri.getPath()
            selectedVideoPath = uri.getPath();
        }
        return selectedVideoPath;
    }
    
}
 
Back
Top Bottom