Category Archives: Android App Development

check if the file exist

first off make a reference file :

File FileName= new File(Environment.getExternalStorageDirectory().getPath()+”/mnt/sdcard/QPSApp/freekick.mp4″);

 

In this example t is aking a reference to a QPS file stored on the SDCard

 

now simply do this to check if it exists:

if(FreeKickVid.exists()){

// it exists

} else {

// it doesnt exist

}

reading data from internal storage

public void read(View view){
String myFilename = “freekick”;
String readData = “”;

openFile(myFilename);

}

public  void openFile(String myFilename) {
String value = “”;
FileInputStream fis;

try {
fis = openFileInput(myFilename);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
TextView myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText(value);
}
}

 

reading data from tag

to read the data from a views tag use this code in bold:

public void passdata(View view) {

 String tag = (String) view.getTag();

 

*the first String defines the variable type

 tag is just the name of my variable

(String) – this is a caster it is the variable type the data from the tag is cast into

view in the example is the name of the view from the start of the function.

 

No need for anything in the brackets as it knows which tag to get because it is the tag that invoked the function

 

Setting text to a view from within an action

To change the text within a view from the java class do this:

 

// declairing the text view to change

        TextView textElement = (TextView) findViewById(R.id.textView);
        textElement.setText(easyPuzzle);

//leave this line to assign a specific text

 

Alternatively use this to change it to a string from the resources folder
    //textElement.setText(R.string.my_string_name);

//leave this line to assign a string resource

Passing data to an action

To send data from one action to another you need to use extras on intents

 

In the first action add this

public void passdata(View view) {
String myText = “Some Text”;
// Do something in response to button
Intent myIntent = new Intent(this, Two.class);
  myIntent.putExtra(“name_of_data_to_retreive”, myText (The string or variable to send);
startActivity(myIntent);

}

 

Then in the action you need the data in onCreate.

Intent intent = getIntent();
        String MyVariable= intent.getExtras().getString(“name_of_data_to_retreive“);

 

The string is now inside MyVariable

 

Now see setting the text within a view for one use of this method

 

Android Development – VideoView Working Code

After searching for hours trying to find a decent tutorial for videoview I finally found one I could use to get this working, so now I will explain it simply, so simply that I can come back here for it when needed and still be able to do it.

Here are the basics, will need to add things to all these files:

  1. MainActivity.java – main class file
  2. activity_main.xml – layout file
  3. VideoViewActivity.java – new class file for videoview
  4. videoview_main.xml – new layout file for videoview
  5. strings.xml – just the strings
  6. AndroidManifest.xml – manifest file

so lets begin.

This is the entire MainActivity.java file required, it has been altered to have 2 buttons and 2 videos:

package com.androidbegin.videostreamtutorial;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button button;
Button buttontwo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);

// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);

// Capture button clicks
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
VideoViewActivity.class);
startActivity(myIntent);
}
});

// Locate the button in activity_main.xml THIS IS A COPY OF THE BUTTON
button = (Button) findViewById(R.id.MyButtonTwo);

// Capture button clicks
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
VideoViewActivityTwo.class);
startActivity(myIntent);
}
});

}

}

 

The important bits out of this are:

Button button;

This is declaring a new variable which is the button which will be pressed to play the video, for more buttons declare more of them.

Then this bit:

     // Get the layout from video_main.xml
        setContentView(R.layout.activity_main);
 
        // Locate the button in activity_main.xml
        button = (Button) findViewById(R.id.MyButton);

//the id of the button to be clicked
 
        // Capture button clicks
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
 
                // Start NewActivity.class
                Intent myIntent = new Intent(MainActivity.this,
                        VideoViewActivity.class);
                startActivity(myIntent);
            }
        }

 

The MainActivity.this is referencing the main java class and shouldnt be changed.

The VideoViewActivity is referencing the videoview.class. I have been changing this for each video as the url is hardcoded in but sure theres a way to pass the URL in the button code.

Now in  activity_main.xml we need to create a button that will trigger the video:

<Button
        android:id=”@+id/MyButton
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_centerHorizontal=”true”
        android:layout_centerVertical=”true”
        android:text=”@string/button” />

It needs to have the id that is mentioned in the MainActivity.java, the rest is just formatting

Now we need to create VideoViewActivity.java

In this is the main code for videoview:

package com.androidbegin.videostreamtutorial;
 
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
 
public class VideoViewActivity extends Activity {
 
    // Declare variables
    ProgressDialog pDialog;
    VideoView videoview;
 
    // Insert your Video URL this is for hosted video
    //String VideoURL = “http://www.root.medlington.co.uk/tutorial/qpstest.mp4”;
 
    // try my video coding – this is an internal android resource
    String VideoURL = “android.resource://com.androidbegin.videostreamtutorial/raw/ski”;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the layout from video_main.xml
        setContentView(R.layout.videoview_main);
        // Find your VideoView in your video_main.xml layout
        videoview = (VideoView) findViewById(R.id.VideoView);
        // Execute StreamVideo AsyncTask
 
        // Create a progressbar
        pDialog = new ProgressDialog(VideoViewActivity.this);
        // Set progressbar title
        pDialog.setTitle(“Android Video Streaming Tutorial”);
        // Set progressbar message
        pDialog.setMessage(“Buffering…”);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();
 
        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);
 
        } catch (Exception e) {
            Log.e(“Error”, e.getMessage());
            e.printStackTrace();
        }
 
        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });
 
    }
 
}

Now create the layout file to display the videoview videoview_main.xml:

Its just a standard layout file with the videoview comp[onant added:

<VideoView
        android:id=”@+id/VideoView”
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
        android:layout_centerHorizontal=”true”
        android:layout_centerVertical=”true” />

 

then simply text to the buttons and such in strings.xml:

 <string name=”app_name”>Video Stream Tutorial</string>
    <string name=”hello_world”>Hello world!</string>
    <string name=”menu_settings”>Settings</string>
    <string name=”action_settings”>Settings</string>
    <string name=”button”>SKI Vid</string>
    <string name=”buttontwo”>QuickPlayTest</string>

 

Lastly we need to add a few lines to the manifest file AndroidManifest.xml

  <uses-permission android:name=”android.permission.INTERNET” >
    </uses-permission>
// this goes under <uses-sdk>

 

Then add a new activity for each instance of videoview:

        <activity android:name=”.VideoViewActivity”            android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
             />
 //optional line for full screen mode