June 28, 2011

How to Attach Images after Launching Camera in Android

public void takePhoto()
{
        //Intent to launch camara
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       
        //Uri creattion to strore images
        mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ "/" +   String.valueOf(System.currentTimeMillis())+ ".jpg"));
                   
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

        startActivityForResult(intent, 1);

}


protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
      
       if(requestCode == 1)
       {
           if(resultCode == RESULT_OK)
           {
                try
               {
                   ivPhoto.setBackgroundResource(0);
                   ivPhoto.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(new File(mImageCaptureUri.getPath()))));
               }
               catch (FileNotFoundException e)
               {               
                   e.printStackTrace();
               }
           }
           else if(resultCode == RESULT_CANCELED)
           {
               Log.d("Result: ", "Launch Cancelled.");
           }
     }
}

Call takePhoto() in the button click. 

Note: 

In the manifast.xml we have to add permissions.android.permission.WRITE_EXTERNAL_STORAGE

android.hardware.camera

Output:










June 24, 2011

How to Remove Dull Background on Active Android Application

While running progress dialog the screen becomes dull. In order to avoid that blur the following code will helpful.

Here i am applying properties to our progress dialog. In this example "lp.dimAmount:" is the attribute to set the dim amount .

pdlg = ProgressDialog.show(getParent(), "", "Loading..");
//Setting the properties
WindowManager.LayoutParams lp = pdlg.getWindow().getAttributes();
lp.dimAmount=0.0f;
//Applying to the screen
pdlg.getWindow().setAttributes(lp);

June 12, 2011

How to Prevent Screen Lock in Android while Running Application


To prevent lock while running the application can be done two ways.

->PowerManager

->WindowManager

Using Power Manager is not a good way. They easily drain battery. So using Window Manager we can avoid the screen locking while application is running. Add the below snippet in your activity to avoid screen lock :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

No permissions required. Put the above code in the activity.

June 11, 2011

Checking Availability of Internet from Android

The following method returns true if internet is available and false if not available.

public boolean checkInternetAvailablity()
{
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnected();
}

June 01, 2011

How to Show Download Progress Dialog in Android

In your application u want to get the data from the server. In this example i am putting all that parsing logic in a thread and showing progress dialog while loading the data. After getting all the data i am dismissing the dialog.
The following example illustrates the same ...


public void parsing()
{
        ProgressDialog pdlg ;
     
       //SHOWING DIALOG
        pdlg = ProgressDialog.show(getParent(), "", "Loading..");
        Thread thr_parsing = new Thread()
        {
            public void run()
            {
               //do the parsing
                runOnUiThread(new Runnable()
                {                   
                    @Override
                    public void run()
                    {   
                            //DISMISSING THE DIALOG
                            pdlg.dismiss();
                    }
                });
            }
        };
        thr_parsing.start();
       
    }