Friday 16 May 2014

What mean by Activities, tasks, and intents in Android ?


Activities In Android:

In Android, an activity is an application component that defines a screen of information and all of the associated actions the user can perform. Your app is a collection of activities, consisting of both the activities you create and those you re-use from other apps.

 

Tasks:

A task is the sequence of activities a user follows to accomplish a goal. A single task can make use of activities from just one app, or may draw on activities from a number of different apps.

 

Intents:

An intent is a mechanism for one app to signal it would like another app's assistance in performing an action. An app's activities can indicate which intents they can respond to. For common intents such as "Share", the user may have many apps installed that can fulfill that request.

 

 


Thursday 17 April 2014

Custom Alert Or Custom Dialog Fragment in Android

I recently can not able to Create Alert Dialog with out it Boarder so i end up to resolved it by Support Dialog Fragment .

You can check my code .



public class AlertActivity extends ActionBarActivity {

    private AlertDialog mAlertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.alert, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {

            //showAlertDialog();
           
            CreateDialogFragment();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_alert,
                    container, false);
            return rootView;
        }
    }

    private void showAlertDialog() {

        AlertDialog.Builder mBuilder = new Builder(new ContextThemeWrapper(
                this, android.R.style.Theme_Translucent_NoTitleBar));// new
                                                                        // Builder(this,android.R.style.Theme_Translucent_NoTitleBar);

        View mView = LayoutInflater.from(this).inflate(
                R.layout.alert_dialog_layout, null);

        mBuilder.setView(mView);
        // mBuilder.setC
        // mBuilder.s

        mAlertDialog = mBuilder.create();

        mAlertDialog.setInverseBackgroundForced(true);
        mAlertDialog.setCanceledOnTouchOutside(true);

        mAlertDialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent);
        mAlertDialog.show();

    }

    private void CreateDialogFragment() {

        View mView = LayoutInflater.from(this).inflate(
                R.layout.alert_dialog_layout, null);
        MyDialogFragment mDialogFragment=new MyDialogFragment(mView);
       
        //mDialogFragment.setStyle(MyDialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar);
        mDialogFragment.setShowsDialog(true);
       
       
        mDialogFragment.show(getSupportFragmentManager(), "dialog");


    }

    public class MyDialogFragment extends DialogFragment {

        private View mView;

        /*
         * public MyDialogFragment newInstance(View mView) {
         *
         * mView=mView; return new MyDialogFragment(); }
         */

        public MyDialogFragment(View mView) {
            this.mView = mView;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
           
            return mView;
        }
       
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
          Dialog dialog = super.onCreateDialog(savedInstanceState);

          // request a window without the title
          dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
          return dialog;
        }
    }

}

Thursday 27 March 2014

Designing And Implementing Android UI For Phones and Tablets

I have found one of Google IO pdf document which give us some fundamental guide for making android UI design for Phone and Tables.


Please have look on pdf from below link.

http://goo.gl/QJFCTG

Tuesday 11 March 2014

How to show PopupWindow in android with background transparent when it show

In this tutorial we will make android popup which will show as drop-down window at particular view point.

Below is Activity class which use to make Popup window on button click.


public class PoupWindowActivity extends Activity implements OnClickListener{


public Button mBtnPopUpWindow;

private PopupWindow mPopupWindow;

private Button mBtnCancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.main_screen_layout);

initView();
}



private void initView(){

mBtnPopUpWindow=(Button)findViewById(R.id.btn_popup);
mBtnPopUpWindow.setOnClickListener(this);


}



@Override
public void onClick(View v) {

switch(v.getId()){


case R.id.btn_popup:

showPopupWindow();

break;

case R.id.btn_cancel:


if(mPopupWindow!=null & mPopupWindow.isShowing()){
mPopupWindow.dismiss();
mPopupWindow=null;
}
break;
}
}


private void showPopupWindow(){

LayoutInflater mLayoutInflater=LayoutInflater.from(this);
View mView=mLayoutInflater.inflate(R.layout.pop_up_layout, null);
mPopupWindow=new PopupWindow(mView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// mPopupWindow=new PopupWindow(mView,,LayoutParams.WRAP_CONTENT);
mPopupWindow.setContentView(mView);
mBtnCancel=(Button) mView.findViewById(R.id.btn_cancel);
mBtnCancel.setOnClickListener(this);
Drawable d = new ColorDrawable(Color.WHITE);
        d.setAlpha(130);
     
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

mPopupWindow.showAsDropDown(mBtnPopUpWindow, 0, 0,Gravity.LEFT);
getWindow().setBackgroundDrawable(d);
mPopupWindow.setOnDismissListener(new OnDismissListener() {

@Override
public void onDismiss() {

Drawable d = new ColorDrawable(Color.WHITE);

getWindow().setBackgroundDrawable(d);
}
});
}


}


Here are xml file use for Activity and Popup window layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal" >

        <Button
         
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/popupwindow"
            android:id="@+id/btn_popup"
            />
    </LinearLayout>

</LinearLayout>



Popup window xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/img_popup_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="30dp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_alerts_and_states_warning" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_below="@+id/img_popup_arrow"
            android:background="@drawable/bg_orange_round" >
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_white_round"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editxt_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right" >

            <Button
                android:id="@+id/btn_search"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/search" />

            <Button
                android:id="@+id/btn_cancel"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:text="@string/cancel" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


Here is output of our popup window:


You can download source code from my drive as well:

https://drive.google.com/file/d/0B-8An4Rd1nmKeVJSejlEYUxwRGc/edit?usp=sharing


Thursday 6 February 2014

Android : Custom Listview with EditText in it and Managing data of Editext


In One of my application we required to have EditText in Listview and i need to manage hot to fetch data from particular Editext when user done Editing after Using TextWatcher interface i can able to do what i required .

So I am sharing my code with you people ,if any one have better approach to work with Edittext in Listview and manage it's data as well then you can share your idear and implementation as well.


As my screen contain only list so i prefer to use List Activity.





public class ListActivity extends android.app.ListActivity {
private ListView mListview;
private final int TOTAL_ITEMS=100; 
private BaseListAdapter mBaseListAdapter;
ArrayList<String> mArrayList=new ArrayList<String>(TOTAL_ITEMS);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_list);
init();
}
private void init(){
mListview=getListView();
for(int i=0;i<TOTAL_ITEMS;i++){
mArrayList.add("");
}
mBaseListAdapter=new BaseListAdapter(ListActivity.this, mArrayList);
mListview.setAdapter(mBaseListAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}

}


Other class required is my Listview adapter class which is below .

public class BaseListAdapter extends BaseAdapter implements OnTouchListener,TextWatcher{
private Context mContext;
private ArrayList<String> mArrayEdittxt;
private LayoutInflater mLayoutInflater;
private int mPosition;
private boolean isTouch=false;
public BaseListAdapter(Context _Context,ArrayList<String> mArrayEditTxtData){
this.mContext=_Context;
mArrayEdittxt=mArrayEditTxtData;
mLayoutInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mArrayEdittxt.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView==null){
mViewHolder=new ViewHolder();
convertView=mLayoutInflater.inflate(R.layout.list_row_items, parent, false);
mViewHolder.mEditxt=(EditText)convertView.findViewById(R.id.editxt);
mViewHolder.mImgView=(ImageView)convertView.findViewById(R.id.imgview);
convertView.setTag(mViewHolder);
}else{
mViewHolder=(ViewHolder) convertView.getTag();
}
isTouch=false;
mViewHolder.mEditxt.addTextChangedListener(BaseListAdapter.this);
mViewHolder.mEditxt.setOnTouchListener(BaseListAdapter.this);
mViewHolder.mEditxt.setTag(position);
mViewHolder.mEditxt.setText(mArrayEdittxt.get(position));
return convertView;
}
public class ViewHolder{
EditText mEditxt;
ImageView mImgView;
}


@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v("LOG_TAG", "onTouch-->");
isTouch=true;
mPosition=(Integer) v.getTag();
return false;
}



@Override
public void afterTextChanged(Editable s) {
Log.v("LOG_TAG", "afterTextChanged -->"+ s.toString() );
if(isTouch){
mArrayEdittxt.set(mPosition, s.toString());
}
}



@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}



@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

}


Below is my listview row xml file .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/editxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/imgview"
        android:ems="10" >

        <requestFocus />
        
    </EditText>

    <ImageView
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/ic_media_play" />

</RelativeLayout>
If any one want to check my source code they can directly download it from my Google Drive -->
https://drive.google.com/file/d/0B-8An4Rd1nmKNXNDTDRqM2FxWFU/edit?usp=sharing



Wednesday 18 September 2013

Face Detection example in andoid from Gallery and by Camera capure image

Hello Every one ,

I recently learn face detection  in android by searching in google and other blogger i found below code and i have made some change in it .

Hope you will like my code and also shared your information in image processing and your expose in face detection in android.


Below is my android Activity code :



public class FaceDetectActivity extends Activity  implements OnClickListener{

        private static final int TAKE_PICTURE_CODE = 100;
        private static final int MAX_FACES = 5;
        
        private static final int SELECT_PICTURE = 6;
       
        private Bitmap cameraBitmap = null;
        private Button btn_TakePhoto;
       
        private Button btn_select_image;
       
        private Button btn_facedetect;
        
        private ImageView imageView;
       
        private int mWidth;
        private int mHeight;
       
        private ProgressDialog mProgressDialog;
       
        private Button btn_select_img;
       
      
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_detect);
       
        initDisplay();
       
        btn_TakePhoto=(Button)findViewById(R.id.take_picture);
        btn_TakePhoto.setOnClickListener(this);
        btn_select_img=(Button)findViewById(R.id.btn_select_gallary);
        btn_select_img.setOnClickListener(this);
       
       
       
    }


   
   
    private void initDisplay(){
       
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        mHeight=metrics.heightPixels;
        mWidth=metrics.widthPixels;
       
        Log.v("LOG_TAG", "Wdith is "+ mWidth +"Height is "+ mHeight);
    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.face_detect, menu);
        return true;
    }

   
   
    private void openCamera(){
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        
        startActivityForResult(intent, TAKE_PICTURE_CODE);
    }
    
    private void processCameraImage(Intent intent){
        setContentView(R.layout.face_detect_layout);
        
        btn_facedetect=(Button)findViewById(R.id.detect_face);
        btn_facedetect.setOnClickListener(this);
        
       
       
        btn_select_image=(Button)findViewById(R.id.btn_pickimag);
       
        btn_select_image.setOnClickListener(this);
       
       
        imageView= (ImageView)findViewById(R.id.image_view);
        
        cameraBitmap = (Bitmap)intent.getExtras().get("data");
        
        imageView.setImageBitmap(cameraBitmap);
    }

    @Override
    public void onClick(View v) {
       
        switch(v.getId()){
       
        case R.id.take_picture:
           
            openCamera();
            break;
           
        case R.id.detect_face:
           
            //detectFaces();
           
            new FaceDetectAsyncTask().execute(cameraBitmap);
           
            break;
           
        case R.id.btn_pickimag:
           
            startImagePicker();
           
            break;
       
           
        case R.id.btn_select_gallary:
           
            startImagePicker();
           
            break;
       
        }
       
    }
   
   
   
    private Object[] detectFaces(Bitmap mBitmap){
      
        Object[] mObject = new Object[2];
       
        if(null != mBitmap){
               
               int width = mBitmap.getWidth();
                int height = mBitmap.getHeight();
                
                FaceDetector detector = new FaceDetector(width, height,FaceDetectActivity.MAX_FACES);
                Face[] faces = new Face[FaceDetectActivity.MAX_FACES];
                
                Bitmap bitmap565 = Bitmap.createBitmap(width, height, Config.RGB_565);
                Paint ditherPaint = new Paint();
                Paint drawPaint = new Paint();
                
                ditherPaint.setDither(true);
                drawPaint.setColor(Color.GREEN);
                drawPaint.setStyle(Paint.Style.STROKE);
                drawPaint.setStrokeWidth(2);
                
                Canvas canvas = new Canvas();
                canvas.setBitmap(bitmap565);
                canvas.drawBitmap(mBitmap, 0, 0, ditherPaint);
                
                int facesFound = detector.findFaces(bitmap565, faces);
                PointF midPoint = new PointF();
                float eyeDistance = 0.0f;
                float confidence = 0.0f;
                
                Log.i("FaceDetector", "Number of faces found: " + facesFound);
                
                if(facesFound > 0)
                {
                        for(int index=0; index<facesFound; ++index){
                                faces[index].getMidPoint(midPoint);
                                eyeDistance = faces[index].eyesDistance();
                                confidence = faces[index].confidence();
                                
                                Log.i("FaceDetector",
                                                "Confidence: " + confidence +
                                                ", Eye distance: " + eyeDistance +
                                                ", Mid Point: (" + midPoint.x + ", " + midPoint.y + ")");
                                
                                canvas.drawRect((int)midPoint.x - eyeDistance ,
                                                                (int)midPoint.y - eyeDistance ,
                                                                (int)midPoint.x + eyeDistance,
                                                                (int)midPoint.y + eyeDistance, drawPaint);
                        }
                }else{
                    //Toast.makeText(this, "No Face Detect", Toast.LENGTH_SHORT).show();
                }
                
                /*String filepath = Environment.getExternalStorageDirectory() + "/facedetect" + System.currentTimeMillis() + ".jpg";
                
                        try {
                                FileOutputStream fos = new FileOutputStream(filepath);
                                
                                bitmap565.compress(CompressFormat.JPEG, 90, fos);
                                
                                fos.flush();
                                fos.close();
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }*/
                        
                       /* ImageView imageView = (ImageView)findViewById(R.id.image_view);
                        
                        imageView.setImageBitmap(bitmap565);*/
               
                mObject[0]=facesFound;
                mObject[1]=bitmap565;
               
                        return mObject;
        }
       
        return mObject;
    }
   
   
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if(TAKE_PICTURE_CODE == requestCode){
                   
                if(Activity.RESULT_OK==resultCode){
                    processCameraImage(data);
                }else{
                    Toast.makeText(this, "You have not capture Image", Toast.LENGTH_SHORT).show();
                   
                     setContentView(R.layout.face_detect_layout);
                    
                        btn_facedetect=(Button)findViewById(R.id.detect_face);
                        btn_facedetect.setOnClickListener(this);
                        
                       
                       
                        btn_select_image=(Button)findViewById(R.id.btn_pickimag);
                       
                        btn_select_image.setOnClickListener(this);
                       
                       
                        imageView= (ImageView)findViewById(R.id.image_view);
                }
               
            }else if(SELECT_PICTURE==requestCode){
               
               
                if(Activity.RESULT_OK==resultCode){
                   
                    Uri selectedImageUri = data.getData();
                    String selectedImagePath = getPath(selectedImageUri);
                   
                   
                    setContentView(R.layout.face_detect_layout);
                   
                    btn_facedetect=(Button)findViewById(R.id.detect_face);
                    btn_facedetect.setOnClickListener(this);
                    
                   
                   
                    btn_select_image=(Button)findViewById(R.id.btn_pickimag);
                   
                    btn_select_image.setOnClickListener(this);
                   
                   
                    imageView= (ImageView)findViewById(R.id.image_view);
                   
                    updateImageFromGallary(selectedImagePath);
                   
                   
                   
                   
                   
                }else{
                   
                }
               
            }
           
           
    }
   
   
    private void updateImageFromGallary(String imgPath){
       
        try{
           
            cameraBitmap=null;
           
            //cameraBitmap=BitmapFactory.decodeFile(imgPath);
           
            cameraBitmap=decodeSampledBitmapFromResource(getResources(),imgPath,mWidth,mHeight);
           
            imageView.setImageBitmap(cameraBitmap);
           
            Toast.makeText(this, "Press Face detect for Face Detection", Toast.LENGTH_SHORT).show();
           
        }catch(OutOfMemoryError outMemeoryError){
           
            Toast.makeText(this, "Unable to Load Image", Toast.LENGTH_SHORT).show();
           
        }
       
       
       
       
    }
   
   
    private void startImagePicker(){
       
        // in onCreate or any event where your want the user to
        // select a file
        /*Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);*/
        Intent intent = new Intent(Intent.ACTION_PICK,
                   android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
       
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);
       
    }
   
   
    public String getPath(Uri uri) {
       
       
       
         Uri selectedImage = uri;
         String[] filePathColumn = {MediaStore.Images.Media.DATA};

         Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        
         cursor.moveToFirst();

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         String filePath = cursor.getString(columnIndex);
       
         return filePath;
       
       /* String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);*/
    }
   
   
    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
   
   
    public static Bitmap decodeSampledBitmapFromResource(Resources res, String path,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
       // BitmapFactory.decodeResource(res, resId, options);
       // BitmapFactory.decodeFile(path);
       
        BitmapFactory.decodeFile(path, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
       //return BitmapFactory.decodeResource(res, resId, options);
       
        return  BitmapFactory.decodeFile(path, options);
    }
   
   
   
    public class FaceDetectAsyncTask extends AsyncTask<Bitmap, Void, Object[]>{

       
       
        @Override
        protected void onPostExecute(Object[] result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
           
           
           
            dismissProgressDialog();
           
            if(result!=null){
               
                if(result[0]!=null){
                   
                    int faceDetect=(Integer) result[0];
                    if(faceDetect>0){
                       
                        ImageView imageView = (ImageView)findViewById(R.id.image_view);
                        
                        imageView.setImageBitmap((Bitmap) result[1]);
                    }else{
                        Toast.makeText(FaceDetectActivity.this, "No Face Detect", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(FaceDetectActivity.this, "No Face Detect", Toast.LENGTH_SHORT).show();
                }
               
               
            }
           
           
           
           
           
        }

        /*@Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
           
             dismissProgressDialog();
           
             ImageView imageView = (ImageView)findViewById(R.id.image_view);
            
             imageView.setImageBitmap(result);
            
           
        }
*/
        @Override
        protected void onPreExecute() {
           
            super.onPreExecute();
           
            showProgressDialog();
        }

        @Override
        protected Object[] doInBackground(Bitmap... params) {
            // TODO Auto-generated method stub
           
            Bitmap mBitmap=params[0];
            Object [] mObjArray;
           
            mObjArray=detectFaces(mBitmap);
           
            return mObjArray;
        }

       
    }
   
   
    private void showProgressDialog(){
       
        if(mProgressDialog!=null && mProgressDialog.isShowing()){
            return ;
        }else{
            mProgressDialog=new ProgressDialog(this);
           
            mProgressDialog.setTitle("Please wait...");
           
            mProgressDialog.setMessage("Detecting face from image");
           
            mProgressDialog.setCancelable(false);
           
            mProgressDialog.show();
        }
    }
   
    private void dismissProgressDialog(){
   
        if(mProgressDialog!=null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
            mProgressDialog=null;
        }
    }
   
   
}


Here are my layout files  code:

activity_face_detection.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FaceDetectActivity"
    android:gravity="center"
     >

     <!-- <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_info"
        android:gravity="center_horizontal"
        android:layout_weight="1.0"
        android:id="@+id/txt_appinfo"
        /> -->
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/take_picture"
        android:layout_margin="5dip"
        android:text="@string/take_picture"
        android:layout_gravity="center_horizontal"
        android:layout_centerHorizontal="true"
       
       
        />
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_select_gallary"
        android:layout_margin="5dip"
        android:text="@string/select_image_from_gallary"
        android:layout_gravity="center_horizontal"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/take_picture"
        />

</RelativeLayout>

face_detect_layout.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
         />
   
   
   
   
    <LinearLayout
       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >
       
         <Button
        android:id="@+id/detect_face"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:text="@string/detect_face"
        android:layout_gravity="center_vertical"
         />
        
          <Button
        android:id="@+id/btn_pickimag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_gravity="center_vertical"
        android:text="@string/select_image_from_gallary" />
       
    </LinearLayout>

  

</LinearLayout>



Wednesday 31 July 2013

Display Image from Redirected URL in android

Today i am sharing one of my analysis for image load in android for redirected url.


Our Goal

We need to display image from URL that itself redirected to another URL.
Hit above URL on your browser, you will be redirected to actual URL like below.

Problem when using third party library like lazy loading

I have seen many library uses HttpURLConnection class to display image from URL.
Like “Universal Image Loader” , “Android-Query ” but these library can't handle URL redirection.

So, you can also manage URL redirection using HttpURLConnection class itself.
Please read below explanation to get more idea and What is best approach.
Now, We have below Http clients which are used to download image from specified URL.

  1. HttpURLConnection / HttpsURLConnection
3. DefaultHttpClient / HttpClient

  • This class does not support URL redirection automatically.
  • We need to write extra code to handle URL redirection.
  • You need to take care manually to close the connection of client.
  • It will handle HTTP to HTTPS URL redirection and vice versa.
  • It will handle only 1 URL redirection.

  1. HttpURLConnection

  • This class does not support URL redirection automatically.
  • We need to write some code by setting property setInstanceFollowRedirects = false to handle URL redirection properly.
  • You need to take care manually to close the connection of client.
  • It will handle HTTP to HTTPS URL redirection and vice versa.
  • This class handles up to 5 redirection.

  1. DefaultHttpClient / HttpClient

  • This class by default support URL redirection automatically.
  • We need not to write any code.
  • No need to close the connection.
  • It will handle HTTP to HTTPS URL redirection and vice versa.
  • This class handles greater than five redirection. I am not able to find maximum limit.

Conclusion

From above explanation, Winner is DefaultHttpClient. We must use DefaultHttpClient class. This class also handles Cookie forward mechanism very well if we use singleton pattern in cookie based authentication process in project.



Now below are code for above example.

Below is my main Activity class.


public class MainActivity extends Activity implements OnClickListener{

private static String TAG = MainActivity.class.getSimpleName();
private Button  btnAndroidHttpClient, btnDownloadHttpURLConnection, btnDownloadDefaultHttpClient;
private ImageView imgView;
private DefaultHttpClient client = null;
/**
* put your URL that itself redirected to another url that is the actual url to download image 
*/
private static final String URL_AndroidHttp = "http://graph.facebook.com/xxxxxxxxxxxx/picture/";
private static final String URL_HttpUrlConnection = "http://wired.ivvy.com/image/display/account/14/file/119806";
private static final String URL_DefaultHttp = "http://graph.facebook.com/xxxxxxxxxxxx/picture/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        btnAndroidHttpClient = (Button) findViewById(R.id.btnDownloadAndroidHttpClient);
        btnAndroidHttpClient.setOnClickListener(this);
        
        btnDownloadHttpURLConnection = (Button)findViewById(R.id.btnDownloadHttpURLConnection);
        btnDownloadHttpURLConnection.setOnClickListener(this);
        
        btnDownloadDefaultHttpClient = (Button) findViewById(R.id.btnDownloadDefaultHttpClient);
        btnDownloadDefaultHttpClient.setOnClickListener(this);
        
        imgView = (ImageView)findViewById(R.id.imgView);
        
    }

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnDownloadAndroidHttpClient:
if(Utils.isNetworkAvailable(MainActivity.this))
{
if(URL_AndroidHttp != null || !URL_AndroidHttp.equalsIgnoreCase(""))
{
new DownloadImageViaAndroidHttpClientTask().execute(URL_AndroidHttp,imgView);
}
else
{
Toast.makeText(MainActivity.this, "Please provide image url.", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "Please check your internet connectivity.", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnDownloadHttpURLConnection:
if(Utils.isNetworkAvailable(MainActivity.this))
{
if(URL_HttpUrlConnection != null || !URL_HttpUrlConnection.equalsIgnoreCase(""))
{
new DownloadImageViaHttpURLConnectionTask().execute(URL_HttpUrlConnection,imgView);
}
else
{
Toast.makeText(MainActivity.this, "Please provide image url.", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "Please check your internet connectivity.", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnDownloadDefaultHttpClient:
if(Utils.isNetworkAvailable(MainActivity.this))
{
if(URL_DefaultHttp != null || !URL_DefaultHttp.equalsIgnoreCase(""))
{
new DownloadImageViaDefaultHttpClientTask().execute(URL_DefaultHttp,imgView);
}
else
{
Toast.makeText(MainActivity.this, "Please provide image url.", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "Please check your internet connectivity.", Toast.LENGTH_SHORT).show();
}
break;
}
}

class DownloadImageViaAndroidHttpClientTask extends AsyncTask<Object, Void, Drawable>
{
private ImageView imgView;
private ProgressDialog dialog;
private AndroidHttpClient androidHttpClient =  AndroidHttpClient.newInstance("Android");
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Please wait...");
dialog.show();
}


@Override
protected Drawable doInBackground(Object... params) {
imgView =  (ImageView) params[1];
try {
   HttpGet httpGet=new HttpGet((String)params[0]);
   HttpResponse httpResponse=androidHttpClient.execute(httpGet);
   androidHttpClient.close();
  
   final int statusCode = httpResponse.getStatusLine().getStatusCode();
   
   if (statusCode != HttpStatus.SC_OK) {
           Header[] headers = httpResponse.getHeaders("Location");
           
           if (headers != null && headers.length != 0) {
               String newUrl =  headers[headers.length - 1].getValue();
               
               AppLog.i(TAG, "newUrl=>"+newUrl);
               
               /**
                * call again with new URL to get image 
               */
               return downloadImage(newUrl);
           } else {
               return null;
           }
       }
}
catch (Exception ex)
{
AppLog.e(TAG, "DownloadImageTask doInBackground() Exception=>"+ex);
}
return null;
}
@Override
protected void onPostExecute(Drawable drawable) {
if (dialog.isShowing()) {
           dialog.dismiss();
       }
if(drawable != null)
{
imgView.setImageDrawable(drawable);
}
}
}
class DownloadImageViaHttpURLConnectionTask extends AsyncTask<Object, Void, Drawable>
{
private ImageView imgView;
private ProgressDialog dialog;
private HttpURLConnection con = null;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Please wait...");
dialog.show();
}


@Override
protected Drawable doInBackground(Object... params) {
imgView =  (ImageView) params[1];
try {
con = (HttpURLConnection)(new URL((String)params[0]).openConnection());
   con.disconnect();
   con.setInstanceFollowRedirects(false);
   con.connect();
   final int responseCode = con.getResponseCode();
   
   AppLog.i(TAG, "responseCode=>"+responseCode);
   
   if (responseCode != HttpStatus.SC_OK) {
    String newUrl = con.getHeaderField("Location");;
           
               AppLog.i(TAG, "newUrl=>"+newUrl);
               
               return downloadImage(newUrl);
   }
}
catch (Exception ex)
{
AppLog.e(TAG, "DownloadImageTask doInBackground() Exception=>"+ex);
}
finally
{
if(con != null)
{
con.disconnect();
con= null;
}
}
return null;
}
@Override
protected void onPostExecute(Drawable drawable) {
if (dialog.isShowing()) {
           dialog.dismiss();
       }
if(drawable != null)
{
imgView.setImageDrawable(drawable);
}
}
}
class DownloadImageViaDefaultHttpClientTask extends AsyncTask<Object, Void, Drawable>
{
private ImageView imgView;
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Please wait...");
dialog.show();
}


@Override
protected Drawable doInBackground(Object... params) {
imgView =  (ImageView) params[1];
try {
/**
* check for whether client os initialized or not. first time it is null, after first request 
* it will not create client. it will use existing initialized client to reduce memory consumption and improve performance.
* It will also used to cookie based implementation if we are using only one instance of client to redirect cookie to further request.  
*/
if(client==null)
       {
        createClient();
       }
        HttpParams httpParams = client.getParams();
        httpParams.setIntParameter("http.connection.timeout", 30000); // 30 SECONDS
        httpParams.setIntParameter("http.socket.timeout", 30000); // 30 SECONDS
   
   HttpGet httpGet=new HttpGet((String)params[0]);
   HttpResponse httpResponse=client.execute(httpGet);
   
   InputStream is=httpResponse.getEntity().getContent();
   Drawable d = Drawable.createFromStream(is, "src name");
   is.close();
   
   return d;
}
catch (Exception ex)
{
AppLog.e(TAG, "DownloadImageTask doInBackground() Exception=>"+ex);
}
return null;
}
@Override
protected void onPostExecute(Drawable drawable) {
if (dialog.isShowing()) {
           dialog.dismiss();
       }
if(drawable != null)
{
imgView.setImageDrawable(drawable);
}
}
}
private static Drawable downloadImage(String stringUrl) {
   URL url = null;
   HttpURLConnection connection = null;
   InputStream inputStream = null;
   
   try {
       url = new URL(stringUrl);
       connection = (HttpURLConnection) url.openConnection();
       connection.setUseCaches(true);
       inputStream = connection.getInputStream();
       
       
   Drawable d = Drawable.createFromStream(inputStream, "src name");
   inputStream.close();
   
       return d;
   } catch (Exception e) {
       AppLog.e(TAG, "Error while retrieving bitmap from " + e);
   } finally {
       if (connection != null) {
           connection.disconnect();
       }
   }
   
   return null;
}
public void createClient() {
   BasicHttpParams params = new BasicHttpParams();
   SchemeRegistry schemeRegistry = new SchemeRegistry();
   schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
   schemeRegistry.register( new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
   ClientConnectionManager ccm = new ThreadSafeClientConnManager( params, schemeRegistry);
   client = new DefaultHttpClient( ccm, params);
}
   
    
}


----------------------------------------------------------------------------------------------------------

public class AppLog {

private static boolean sFlagDebug= true;
private static boolean sFlagInfo= true;
private static boolean sFlagErr= true;
/**
* THis will print in Blue
* @param tag
* @param message
*/
public  static void d(String tag, String message){
if(sFlagDebug)
{
Log.d(tag,message);
}
}
/**
* This will print in Green
* @param tag
* @param message
*/
public  static void i(String tag, String message){
if(sFlagInfo)
{
Log.i(tag,message);
}
}
/**
* This will print in Error
* @param tag
* @param message
*/
public  static void e(String tag, String message){
if(sFlagErr)
{
Log.e(tag,message);
}
}
}


---------------------------------------------------------------------------------------------------------

public class Utils {

private static final String TAG = Utils.class.getSimpleName();


/**
* Check Internet connection flag.
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}

}