Can't Play Video in Gallery

All your problems with Audio, Video and Images.

Can't Play Video in Gallery

Postby micadeyeye » Sat Aug 06, 2011 12:26 pm

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: Select all
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;
    }
   
}
micadeyeye
Freshman
Freshman
 
Posts: 5
Joined: Sat Aug 06, 2011 12:22 pm

Top

Re: Can't Play Video in Gallery

Postby ramesh.kec.85 » Sat Aug 06, 2011 2:08 pm

Try this following code...i have played succefully in my device
------------


import java.math.BigDecimal;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoListActivity extends ListActivity {
private MediaCursorAdapter mediaAdapter = null;

private String currentFile = "";
VideoView video;
MediaController mediaController;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
video = (VideoView) findViewById(R.id.videoView1);
mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setKeepScreenOn(true);


Cursor cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);

if (null != cursor) {
cursor.moveToFirst();

mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem,
cursor);

setListAdapter(mediaAdapter);


}
}

@Override
protected void onListItemClick(ListView list, View view, int position,
long id) {
super.onListItemClick(list, view, position, id);

currentFile = (String) view.getTag();
video.setVideoPath(currentFile);
video.start();
video.requestFocus();
}

@Override
protected void onDestroy() {
super.onDestroy();

}

private class MediaCursorAdapter extends SimpleCursorAdapter {

public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c, new String[] {
MediaStore.Video.VideoColumns.DISPLAY_NAME,
MediaStore.Video.VideoColumns.ARTIST,
MediaStore.Video.VideoColumns.DURATION }, new int[] {
R.id.displayname, R.id.title, R.id.duration });
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);

name.setText(cursor.getString(cursor
.getColumnIndex(MediaStore.Video.VideoColumns.DISPLAY_NAME)));

String str=cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.ARTIST));

if(str.equals("<unknown>"))
title.setText(" ");
else
title.setText(""
+ cursor.getString(cursor
.getColumnIndex(MediaStore.Video.VideoColumns.ARTIST)));

long durationInMs = Long.parseLong(cursor.getString(cursor
.getColumnIndex(MediaStore.Video.VideoColumns.DURATION)));

double durationInMin = ((double) durationInMs / 1000.0) / 60.0;

durationInMin = new BigDecimal(Double.toString(durationInMin))
.setScale(2, BigDecimal.ROUND_UP).doubleValue();

duration.setText("" + durationInMin);

view.setTag(cursor.getString(cursor
.getColumnIndex(MediaStore.Video.VideoColumns.DATA)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);
bindView(v, context, cursor);
return v;
}
}

}
Thanks

Regards
--R@mesh
ramesh.kec.85
Developer
Developer
 
Posts: 27
Joined: Sat Jul 30, 2011 10:05 am
Location: Ahmedabad

Re: Can't Play Video in Gallery

Postby micadeyeye » Sat Aug 06, 2011 3:11 pm

Could you please send me your layout (x.xml) file?
micadeyeye
Freshman
Freshman
 
Posts: 5
Joined: Sat Aug 06, 2011 12:22 pm

Re: Can't Play Video in Gallery

Postby micadeyeye » Sat Aug 06, 2011 4:04 pm

I will appreciate it if you could send me the application source code. It's pretty difficult getting a hang of the code you sent me. BTW, thanks once again
micadeyeye
Freshman
Freshman
 
Posts: 5
Joined: Sat Aug 06, 2011 12:22 pm

Re: Can't Play Video in Gallery

Postby ramesh.kec.85 » Mon Aug 08, 2011 5:29 am

micadeyeye wrote:I will appreciate it if you could send me the application source code. It's pretty difficult getting a hang of the code you sent me. BTW, thanks once again
Attachments
x.xml.rar
This is my xml code...
(1.09 KiB) Downloaded 25 times
Thanks

Regards
--R@mesh
ramesh.kec.85
Developer
Developer
 
Posts: 27
Joined: Sat Jul 30, 2011 10:05 am
Location: Ahmedabad

Re: Can't Upload Video in Gallery

Postby micadeyeye » Mon Aug 08, 2011 6:19 pm

Hi,
I seem to be having a buffer-related problem each time I try to upload a video to a server from my phone gallery. The buffer size (on debugging) has always been 123. Below is my code. Has anyone got an idea of what could be wrong?

//check if sd media is available
checkExternalMedia();
String fileToUpload = currentFile;
String existingFileName = fileToUpload;
//String urlServer = "http://10.42.43.11/test/handle_upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead;
int maxBufferSize = 1 * 1024 * 1024;

try {
File uploadFile = new File(existingFileName);
FileInputStream fileInputStream = new FileInputStream(uploadFile);
//HttpURLConnection conn = null;

// URL to upload to
URL connectURL = new URL("http://10.42.43.13/handle_upload.php?filename="+fileToUpload);

// connectURL is a URL object
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();

// allow inputs
conn.setDoInput(true);

// allow outputs
conn.setDoOutput(true);

// don't use a cached copy
conn.setUseCaches(false);

// use a post method
conn.setRequestMethod("POST");

// set post headers
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);


//get file name from path
String realFileName = existingFileName;
int idx = realFileName.lastIndexOf("/");
realFileName.substring(idx+1, realFileName.length());

// open data output stream
//DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
DataOutputStream dos = new DataOutputStream(new FileOutputStream(realFileName));
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+existingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);

// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];

// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
// Upload file part(s)
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}

// Send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Close streams
fileInputStream.close();
dos.flush();

InputStream is = conn.getInputStream();
int ch;

StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
b.append( (char)ch );
micadeyeye
Freshman
Freshman
 
Posts: 5
Joined: Sat Aug 06, 2011 12:22 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 12 guests