Thursday, 6 September 2012

How To Pick Image From Gallery : Android


1)First screen shows user with and Image view and a button to loan Picture.
2)On click of “Load Picture” button, user will be redirected to Android’s Image Gallery where she can select one image.
3) Once the image is selected, the image will be loaded in Image view on main screen.

Step1)Create Basic Android Project in Eclipse

Create a Hello World Android project in Eclipse. Go to New > Project > Android Project. Give the project name as ImageGalleryDemo .

Step 2) Change the Layout

we need simple layout. One Image view to display user selected image and one button to trigger Image gallery.
Open layout/main.xml in your android project and replace its content with following:
 Path:-    res/layout/main.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" >
    <ImageView
        android:id="@+id/imgView"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="@android:color/black"
        android:layout_weight="1">
    </ImageView>
    <Button
        android:id="@+id/buttonGallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:text="Show Gallery"
    />

</RelativeLayout>

  Note the id of Image view is imgView and that of Button is buttonGallery.

Step 3: Android Code to trigger Image Gallery Intent(Open Gallery)

 Now we need to write some code to actually handle the button click. On click of Show Gallery button, we need to trigger the intent for Image Gallery.

    Intent i = new Intent(
                Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);

 We passed an integer RESULT_LOAD_IMAGE to startActivityForResult() method. This is to handle the result back when an image is selected from Image Gallery.

Step 4: Getting back selected Image details in Our Activity

 Once user will select an image, the method onActivityResult() of our main activity will be called. We need to handle the data in this method as follows:
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
       
         if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
             Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
             cursor.close();
                 
             // String picturePath contains the path of selected Image
    }
  The method onActivityResult gets called once an Image is selected. In this method, we check if the activity that was triggered was indeed Image Gallery (It is common to trigger different intents from the same activity and expects result from each). For this we used RESULT_LOAD_IMAGE integer that we passed previously to startActivityForResult() method.

Below is the Final Code  - 

public class MainActivity extends Activity implements OnClickListener {
Button galleryBtn;
int RESULT_LOAD_IMAGE=1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * Gallery Btn refrenced From main.xml              umesh
         */
        galleryBtn=(Button) findViewById(R.id.buttonGallery);
        galleryBtn.setOnClickListener(this);
      
    }
 /*
  *  On click of gallery Btn trigger Android Gallery
  */
    @Override
    public void onClick(View v) {
                Intent i = new Intent(
                Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
      
    } // on Click View
  
  
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        
 if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
             Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
             cursor.close();
             // String picturePath contains the path of selected Image
           
             // Show the Selected Image on ImageView
             ImageView imageView = (ImageView) findViewById(R.id.imgView);
             imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
          
       }  // if Loop
  
    }    // on ActivityResult

} // MainActivity

Screen shots of Pick Image From Gallery

1) 


2)select an image from Image Gallery
3)selected image,  will be displayed on our main activity
 
 Thanks,

Wednesday, 5 September 2012

Android: Take Emulator Screen Shots in Eclipse

If you are an Android developer then you are already taking lot of screen shots of your app. For documentation purpose or for putting it in Google Play market. Taking screen shots of Android in phone is easy in today’s smart phones like Samsung Galaxy series. But most of the time we would need to grab the screen from Android emulator. 

Here’s a simple but very useful trick to capture screen shot of Android application via Eclipse. For this you must have installed Android’s ADT plugin in Eclipse.

Follow these simple steps to capture the current screen of your current Emulator.
In eclipse, Open Windows > Show View > Others… (Shortcut Alt + Shift + Q, Q)


From the Show View dialog, open Android and select Devices. (It Show Ur Device List)

 


This will open a new Devices view in your eclipse. Select the Emulator that is running currently and that you want to capture screen of and click Camera icon on right side.


This will open Device Screen Capture dialog. Press Refresh button to capture current screen and press Save to save it in PNG format on disk.






Thanks,   



Thursday, 7 June 2012

Development with Android

1)What is Android?


Android System - 


Android is an operating system based on Linux with a Java programming interface.
The Android Software Development Kit provides all necessary tools to develop Android applications. This includes a compiler, debugger and a device emulator and its own virtual machine to run Android programs.
Android applications consist of different components and can re-use components of other applications. 
Android provides a rich user interface library, allows background processing, supports 2D and 3D graphics using the OpenGL libraries , access to the file system and provides an embedded SQLite database.

Android Development Tools-

Google provides the Android Development Tools (ADT) to develop Android applications with Eclipse.
ADT is a set of components (plug-ins) which extend with the Eclipse IDE with Android development capabilities.
ADT provides android device emulator, so that application can be tested on android emulator. 

Android components-

The following gives most important Android components.          -Activity
          -Intents
          -ContentProvider
          -Services
          -BroadcastReceiver
          -Views

Dalvik Virtual Machine-

Dalvik virtual machine (DVM) is optimized visual machine for android platform to run java application.
Every application loads DVM in its own address space.

AndroidManifest.xml-

This XML file contains all the configuration in the application.like all activities,intents,receivers,services etc.
It also contains required Permissions.
The <application>  inside the AndroidManifest.xml defines the Project as an application.
The tag <activity> defines an Activity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ucrules.androidmanifest"
    android:versionCode="1"
    android:versionName="1.0" >
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 <uses-sdk android:minSdkVersion="9" />
 <uses-permission android:name="android.permission.INTERNET"/>
</manifest>


Security and permissions-

Android System will create a unique user and group ID for every android application.
Each application is private to generated user.
Android also contains a permission system. Android predefines permissions for applications certain tasks.
Android application declares permissions in its AndroidManifest.xml configuration file.

R.java and Resources-

It stands for generated java files for all the resources.
R.java (Resource) will be created to reference all the resources from java code.
R.java is automatically created & will be updated automatically when a need resource is added.
NOTE:-(Do Not Modify R.java Manually)


Activities and Lifecycle-

The Android system controls the lifecycle of your application. At any time the Android system may stop or destroy your application.
-Resume - Activity is foreground - onResume();
-Pause - Activity is in paused state - onPause();
-Stop - Activity is in stopped state  - onStop();


DDMS - (Dalvik Debugging Monitoring System)
It is used to monitor all the activities tasks,heap memory etc.
It provides Logcat which can log message for Android.

5554 is the First Starting code of Android Emulator , which runs on telnet server.