Monday, August 27, 2012

Write Text file in Android


public static void doWriteFile(String filename, String data)
{
File file = new File(Environment.getExternalStorageDirectory().toString(),filename);
FileOutputStream fo = null;
try {
fo = new FileOutputStream(file);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(data.getBytes());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Monday, May 7, 2012

isAlpha function in JAVA

This function will return true. if a string contain character value.

like String s="123 Pawan";

public static boolean isAlpha(String s)
{
boolean check=false;
int p=0;

String[] _sc =
{
"a","b","c","d","e","f","g","h","i","j",
"k","l","m","n","o","p","q","r","s","t",
"u","v","w","x","y","z",

"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z",

"~","!","@","#","$","%","^","&",
"*","(",")","-","_","+","=",
",",".","/","?","<",">",":",";","'","|","\",","]","}","[","{","*",
};
for(int i=0;i<_sc.length;i++)
{
String sp=_sc[i];
if(s.indexOf(sp)!=-1)
{
p++;
break;
}
}
if(p!=0)
{
check=true;
}
return check;
}

Wednesday, May 2, 2012

Download gif image from server in BlackBerry


public static Bitmap downloadImage()
     {
           InputStream iStream = null;
           EncodedImage bitmap;
           Bitmap bmp = null ;
           HttpConnection httpConnection = null;

           try
           {
                httpConnection = (HttpConnection) Connector.open("http://www.google.com/ig/images/weather/mostly_cloudy.gif"
                           +ConnectionInfo.getInstance().getConnectionParameters(), Connector.READ_WRITE);

                httpConnection.setRequestMethod(HttpConnection.GET);
                int responseCode = httpConnection.getResponseCode();
                if (responseCode == HttpConnection.HTTP_OK) {

                     iStream = httpConnection.openInputStream();

                     byte[] imageData =  IOUtilities.streamToBytes(iStream);
                     bitmap = EncodedImage.createEncodedImage(imageData, 0, imageData.length);
                     bmp = bitmap.getBitmap();
                     Dialog.alert(""+bmp);
                }
           }
           catch (Exception e)
           {   
                System.out.println("error:"+e.getMessage());
           }
           return bmp;                 
     }

Friday, April 27, 2012

Download image from server in BlackBerry

 public static Bitmap downloadImage(String url)
{
InputStream iStream = null;
EncodedImage bitmap;
    HttpConnection httpConnection = null;
                                                               
        try
        {
                    httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
        
            httpConnection.setRequestMethod(HttpConnection.GET);
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpConnection.HTTP_OK) {
               
           
                iStream = httpConnection.openInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
               
                byte[] buffer = new byte[256];
                int len = 0, imageSize = 0;
               
                while (-1 != (len = iStream.read(buffer))) {
                    byteArrayOutputStream.write(buffer);
                    imageSize += len;
                }
               
                byteArrayOutputStream.flush();
                byte[] imageData = byteArrayOutputStream.toByteArray();
                byteArrayOutputStream.close();
                bitmap = EncodedImage.createEncodedImage(imageData, 0, imageSize);                                      
                Bitmap bmp = bitmap.getBitmap();
 
returnbmp;
            }
            }
            catch (Exception e)
            {                      
            }
return null;                  
}

Override Back Key in BlackBerry


        public boolean keyChar(char key, int status, int time)
        {
                boolean retval = false;
                if(key == Characters.ESCAPE)
                {
                        retval = true;
                        int response = Dialog.ask(Dialog.D_YES_NO, "Do you really want exit??");
                        if(response == Dialog.YES)
                                System.exit(0);
                }
                else if(key == Characters.ENTER)
                {
                        navigationClick(status, time);
                        return true;
                }
                else
                {
                        retval = super.keyChar(key,status,time);
                }
                return retval;
        }

Show Image from SDCard in Android


File imageFile = new File("/mnt/sdcard/pawan.jpg");
if(imageFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

use this myBitmap object anywhere.
}