Sunday, April 29, 2012

Android - Open Gallery For Image

//Open Gallery To Select Image
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);


//To Show Image After Selection or Get its Path
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode) {
    case 0:
        if(resultCode == RESULT_OK){ 
            Uri selectedImage = imageReturnedIntent.getData();
            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); // file path of selected image
            cursor.close();
                   //  Convert file path into bitmap image using below line.
            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
          
                   // put  bitmapimage in your imageview
            yourimgView.setImageBitmap(yourSelectedImage);
        }
    }
}

Android - Adding External JARS in Your Project

A Best way to add External Jars to your Anroid Project or any Java project is:
  1. Create a folder call 'lib' into you project root folder
  2. Copy your Jar files to the lib folder
  3. Now right click on the Jar file and Build Path > Add to Build Path, this will create a folder called 'Refrenced Library' into you project, and your are done
By do doing like this, whenever you transfer you project, you will not miss you libraries which are being referenced to some space on your Hard drive.