Archive for the ‘Uncategorized’ Category

Shifted My BLOG to BoomTech.in

Posted: December 6, 2011 in Uncategorized

This blog is shifted to http://boomtech.in

click here to go to Home page

boomtech.in

Android- Play video from sdcard

Posted: September 19, 2011 in Uncategorized

below code is play any video from sdard, you just need to give file path.

String filepath = "mnt/sdcard/a.mp4";
VideoView vv = new VideoView(getApplicationContext());
setContentView(vv);
vv.setVideoPath(filepath);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();

Android – Text To Speech

Posted: September 19, 2011 in Uncategorized

Below code is for converting text to speech, here i hav taken a simple example which will convert your text in (English) speech, you can change it in any language which will support.

import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

public class TextToSpeechActivity extends Activity implements OnInitListener {
/** Called when the activity is first created. */
TextToSpeech tts; String text="How Are You Boom?";

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

tts = new TextToSpeech(this,
this // TextToSpeech.OnInitListener
); }

@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
// Set preferred language to US english.
// Note that a language may not be available, and the result will indicate this.
int result = tts.setLanguage(Locale.ENGLISH);
// Try this someday for some interesting results.
// int result mTts.setLanguage(Locale.FRANCE);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e("Text to speech------", "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.

// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
// Greet the user.
tts.speak(text,
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}
} else {
// Initialization failed.
Log.e("text To speech-----", "Could not initialize TextToSpeech.");
}

}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}

below code is just example of gesture detection on android device surface, but for that here we hav “gestures” raw library file for gesture identification, so you must have some gesture library file for that


import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;

public class GestureActivity extends Activity implements OnGesturePerformedListener {
    /** Called when the activity is first created. */
    private GestureLibrary gLib;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gLib = GestureLibraries.fromRawResource(this,R.raw.gestures);
        if (!gLib.load()) {
            Log.i("Gesture Lib", "could not load gesture library");
            finish();
        }
        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);

        gestures.addOnGesturePerformedListener(this);

    }

    @Override
    public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
        // TODO Auto-generated method stub
        ArrayList<Prediction> predictions = gLib.recognize(arg1);
        if(predictions.size() > 0)
        {
            Prediction prediction = predictions.get(0);
            if(prediction.score > 1.0){
                Log.i("GESTURE DETECTED"+prediction.score,prediction.name);

        }
        }
    }
}

Android Device- Status bar Notification

Notification

Notification

This is a sample code for displaying notification on status bar of Android Device. You can use this code in service file or in broadcast receiver file or in activity file..

——————————————————————————————————————————————

private NotificationManager mManager;
mManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,"Notification!!", System.currentTimeMillis());
Intent notificationIntent = new Intent(context, SMSActivity.class); 
// if u want to redirect to any activity , u can use intent ike this, which will call when click on notification
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.contentview);

contentView.setImageViewResource(R.id.image, R.drawable.icon);     // for displaying icon on notification
contentView.setTextViewText(R.id.text, "Your text here");   // for displaying text on notification

notification.contentView = contentView;
mManager.notify(500, notification);

—————————————————————————————————————————————-

Below is the content view which used in this example

—————————————————————————————————————————————–

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="3dp"
              >
    <ImageView android:id="@+id/image"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_marginRight="10dp"
              />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#000"
              />
</LinearLayout>

-------------------------------------------------------------------------------------------------
end // ask me if u have any query :)
----------------------------------------------------------------------------------------

This is the easiest way to load bitmaps from the sdcard. Simply pass the path to the image to BitmapFactory.decodeFile() and let the Android SDK do the rest.

Activity File

package higherpass.TestImages;
import android.app.Activity;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.widget.ImageView;
public class TestImages extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 ImageView image = (ImageView) findViewById(R.id.test_image);
 Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
 image.setImageBitmap(bMap);
 }
 }

All this code does is load the image test2.png that we previously copied to the sdcard. The BitmapFactory creates a bitmap object with this image and we use the ImageView.setImageBitmap() method to update the ImageView component.

This sample is for executing click event of button.. you can implement same for any other view also like image view or anything..

————————————————————————

test.java (Activity)

-------------------------------------------------------------------------
 package com.example.test;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 public class test extends Activity implements OnClickListener{
 Button btn;
 int count=0;
 public void onCreate(Bundle icicle)
 {
super.onCreate(icicle);
 setContentView(R.layout.main);
 btn= (Button)findViewById(R.id.button1);
 btn.setOnClickListener(this);
 }

public void onClick(View v) {
// TODO Auto-generated method stub
btn.setText(“Clicked “+ ++count + ” Times”);
}
}
——————————–
main.xml
——————————–

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linear1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:text="ClickMe" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

———————————————

end of code

————————————————-