Media activity : Intent « Core Class « Android






Media activity

     


package app.test;

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

public class Test extends Activity implements View.OnClickListener {
 
    private static final int REQUEST_AUDIO = 1;
    private static final int REQUEST_VIDEO = 2;
    private static final int REQUEST_IMAGE = 3;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button images = (Button)findViewById(R.id.imageButton);
        images.setOnClickListener(this);
        Button videos = (Button)findViewById(R.id.videoButton);
        videos.setOnClickListener(this);
        Button audio = (Button)findViewById(R.id.audioButton);
        audio.setOnClickListener(this);
        
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
        if(resultCode == Activity.RESULT_OK) {
            Uri selectedContent = data.getData();
            if(requestCode == REQUEST_IMAGE) {
                //Display the image
            }
            if(requestCode == REQUEST_VIDEO) {
                //Play the video clip
            }
            if(requestCode == REQUEST_AUDIO) {
                //Play the audio clip
            }
        }
    }
    
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        switch(v.getId()) {
        case R.id.imageButton:
            intent.setType("image/*");
            startActivityForResult(intent, REQUEST_IMAGE);
            return;
        case R.id.videoButton:
            intent.setType("video/*");
            startActivityForResult(intent, REQUEST_VIDEO);
            return;
        case R.id.audioButton:
            intent.setType("audio/*");
            startActivityForResult(intent, REQUEST_AUDIO);
            return;
        default:
            return;
        }
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button
    android:id="@+id/imageButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Images"
  />
  <Button
    android:id="@+id/videoButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Video"
  />
  <Button
    android:id="@+id/audioButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Audio"
  />
</LinearLayout>

   
    
    
    
    
  








Related examples in the same category

1.Using Intent to open other Activity
2.Create Intent to open a Uri
3.extends IntentService
4.Phone Intent
5.Map Intent
6.Market Intent
7.Load Activity with Intent
8.Using Intent to make another phone call
9.extends IntentService to create your own Intent
10.Adding data bundle to Intent
11.Using Intent to show other Activities
12.Start Intent with Utility class
13.Load library Activity with Intent
14.Capture Image with Intent
15.Intent.ACTION_MEDIA_MOUNTED
16.Using Intent to record audio
17.Video Player Intent
18.Video Capture Intent
19.Implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
20.Example of various Intent flags to modify the activity stack.
21.Sample code that invokes the speech recognition intent API.
22.extends IntentService to upload a file
23.factory class for generating various intents
24.Open Web Page Intent
25.Is Intent Available
26.start MMS Intent
27.Access the Internet
28.Pdf viewer
29.Rotation One Demo