18 Jun

Camera Picture Taken on Android

public class MakePhotoActivity extends Activity {

	private final static String DEBUG_TAG = "MakePhotoActivity"; 
	private Camera camera; 
	private int cameraId = 0;
        public void onClick(View view) { 
	    camera.takePicture(null, null, 
	    new PhotoHandler(getApplicationContext())); 
        }

        protected void onPause() { 
	    if (camera != null) { 
		camera.release(); 
		camera = null; 
	    } 
	    super.onPause(); 
        }
        private int findFrontFacingCamera() { 
	    int cameraId = -1; 

	    int numberOfCameras = Camera.getNumberOfCameras(); 
            for (int i = 0; i < numberOfCameras; i++) { 	
                CameraInfo info = new CameraInfo(); 	
                Camera.getCameraInfo(i, info); 
		if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
			Log.d(DEBUG_TAG, "Camera found"); 
			cameraId = i; break; 
		} 
	} 
	return cameraId; 
}
public class PhotoHandler implements PictureCallback
{ 
	
	private final Context context; 
	public PhotoHandler(Context context) {
		this.context = context; 
	}
        private File getDir() { 
	File sdDir = Environment
		.getExternalStoragePublicDirectory(
		Environment.DIRECTORY_PICTURES); 
	return new File(sdDir, &quot;CameraAPIDemo&quot;); 
}
public void onPictureTaken(
  byte[] data, Camera camera) { 

	File pictureFileDir = getDir(); 
	if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { 	
              Log.d(Constants.DEBUG_TAG, 
		  "Can't create directory to save image."); 
		Toast.makeText(context, 
		  "Can't create directory to save image.", 
		  Toast.LENGTH_LONG).show(); 
	return; 
} 

18 Jun

Video Recording On Android

public class VideoCapture extends Activity 
	implements OnClickListener, 
	SurfaceHolder.Callback {    
     MediaRecorder recorder;
     SurfaceHolder holder;
     boolean recording = false;

public void surfaceCreated(SurfaceHolder holder) {
	prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, 
	int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {    
        if (recording) {        
                  recorder.stop();        
                  recording = false;    
        }    
        recorder.release();    
        finish();
}
private void prepareRecorder() {  recorder.setPreviewDisplay(holder.getSurface());    try {        recorder.prepare();    } catch (IllegalStateException e) {        e.printStackTrace();        finish();    } catch (IOException e) {        e.printStackTrace();        finish();    }}
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(
		WindowManager.LayoutParams.FLAG_FULLSCREEN,
 		WindowManager.LayoutParams.FLAG_FULLSCREEN);
	setRequestedOrientation(
		ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);recorder = new MediaRecorder();initRecorder();setContentView(R.layout.main);

	SurfaceView cameraView = 
		(SurfaceView)findViewById(R.id.CameraView);
	holder = cameraView.getHolder();
	holder.addCallback(this);
  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);cameraView.setClickable(true);cameraView.setOnClickListener(this);
}
private void initRecorder() {
	recorder.setAudioSource(
		MediaRecorder.AudioSource.DEFAULT);
	recorder.setVideoSource(
		MediaRecorder.VideoSource.DEFAULT);
	CamcorderProfile cpHigh = CamcorderProfile
		.get(CamcorderProfile.QUALITY_HIGH);
	recorder.setProfile(cpHigh);
	recorder.setOutputFile("/sdcard/
		videocapture_example.mp4");
	recorder.setMaxDuration(50000); // 50 s
         recorder.setMaxFileSize(5000000); // 5 mb
}
public void onClick(View v) {    
        if (recording) {        
              recorder.stop();        
              recording = false;

	     // selalu panggil init untuk record ulang        
              initRecorder();        
               prepareRecorder();    
        } else {        
             recording = true;        
             recorder.start();    
        }
}

18 Jun

Read Write SD Card on Android

pertama yang harus dilakukan adalah membuka AndroidManifest.xml dan tambahkan Permission berikut:

android.permission.WRITE_EXTERNAL_STORAGE

Cara Menulis Data ke File SD Card

File file = new File("/sdcard/mysdfile.txt");
file.createNewFile();
FileOutputStream fos = 
		new FileOutputStream(file); 

OutputStreamWriter osw = 
		new OutputStreamWriter(fos);
osw.append(txtData.getText());
osw.close();
fos.close();

Cara Membaca Data dari File SD Card

File file = new File("/sdcard/mysdfile.txt");
FileInputStream fis = 
	new FileInputStream(file);
BufferedReader myReader = 
	new BufferedReader(new InputStreamReader(fis)); 
String aDataRow = ""; 
String aBuffer = ""; 
while ((aDataRow = myReader.readLine()) != null) {
	aBuffer += aDataRow + "\n"; 
} 
txtData.setText(aBuffer); 
myReader.close();

28 May

Android – Streaming Youtube

@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.main); 
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=...")));
}

public void onPrepared(MediaPlayer mp) { 
    // TODO Auto-generated method stub
}

28 May

Android – Video Player

public void videoPlayer(String path, String fileName, boolean autoplay){
    //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(Uri.parse(path+"/"+fileName));
    //get focus, before playing the video.
    videoHolder.requestFocus();
    if(autoplay){
        videoHolder.start();
    }
 
 }

28 May

Android – Audio Recorder

Kita dapat membuat recorder di dalam Android dengan cara:

Pertama tambahkan Permission

<uses-permission android:name="android.permission.RECORD_AUDIO“ />

Kedua, buat objek media recorder

final MediaRecorder recorder = new MediaRecorder();

Ketiga, Method start record

public void start() throws IOException { 
	String state = android.os.Environment.getExternalStorageState(); if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
		throw new IOException("SD Card is not mounted. It is " + state + "."); 
	} 
	
     File directory = new File(path).getParentFile(); 
	if (!directory.exists() && !directory.mkdirs()) { 
		throw new IOException("Path to file could not be created."); 
	} 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(path); 
	recorder.prepare(); 
	recorder.start(); 
}

Keempat, Method stop record

public void stop() throws IOException { 
	recorder.stop(); 
	recorder.release(); 
}