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.
}

Twitter login page content is not displayed properly.


Hi All
If the login page content is not displayed properly. In BlackBerry
Then plz follow this link.
Please, use BrowserFieldOAuthDialogWrapper in place of BrowserContentManagerOAuthDialogWrapper class. It works fine for me.
I got this success by the help of @ernandesmjr Ernandes Mourao Jr
If any find any issue. please let me know.
Thanks Ernandes

Friday, April 20, 2012

Delete file from SDCard in Android



String path="/mnt/sdcard/dhoom.mp3";
   File file = new File(path);
   boolean test1 = file.exists();
   String yes;
   if(test1==true)
   {
    boolean test = file.delete();
    System.out.println("done");
   }
   else
   {
    System.out.println("not exist file");
   }

Add this line in manifest also.

   

Download image from server in Android



public class DownloadImage {
 public static Bitmap downloadImg()
 {
  URL url = null;
  Bitmap _bitmap = null;
  try
  {
   url = new URL("https://www.google.co.in/ig/images/weather/cloudy.gif");
   _bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  }
  catch (MalformedURLException e)
  {
   e.printStackTrace();
  }
  catch (IOException e)
  {

   e.printStackTrace();
  }
  return _bitmap;
 }
}

Check the connection Availability in BlackBerry Device:



if(CoverageInfo.getCoverageStatus()==CoverageInfo.COVERAGE_NONE)
{
Dialog.alert("No Connection: ");
}
else
{
Dialog.alert("Connection: ");
}

Scale the image



public static Bitmap getScaledBitmapImage(String imagename,int wd,int ht)
{
EncodedImage image = EncodedImage.getEncodedImageResource(imagename);
if(image.getWidth()==wd &amp;&amp; image.getHeight()==ht)
return image.getBitmap();
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int width = wd;
if(width == -1)
width=image.getWidth();
int height = ht;
if(height == -1)
height=image.getHeight();
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return image.getBitmap();
}

Return the Document object from by any XML file.





public static Document getDocument(String data)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document document = null;
try {
document = builder.parse(new ByteArrayInputStream(data.getBytes("UTF-8")));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.getDocumentElement().normalize();

return document;
}

Get the Byte from by any file.



public static byte[] returnByte(String path)
{
FileConnection fconn;
int available = 0;
InputStream input = null;
byte[] data=null;
try
{
fconn = (FileConnection)Connector.open(path);
if(fconn.exists()==true)
{
input = fconn.openInputStream();
available = input.available();
int fSz = (int)fconn.fileSize();
data = new byte[fSz];
input.read(data, 0, fSz);
}
}
catch(Exception e)
{
Dialog.alert("Error");
}
return data;
}

Delete file from SDCard in BlackBerry...




public static String path = "file:///SDCard/BlackBerry/music/Pawan.amr";

public static void deleteFile()
{
try
{
FileConnection fconn = (FileConnection) Connector.open(path,Connector.READ_WRITE);
if (fconn.exists())
{
fconn.delete();
fconn.close();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}