Monday, 15 October 2012

Create your own camera app and take images android

  1. Create a SurfaceView as follow (it will interact with camera)

    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import android.content.Context;
    import android.hardware.Camera;
    import android.hardware.Camera.PreviewCallback;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class CameraView extends SurfaceView implements 
    SurfaceHolder.Callback
    {
     SurfaceHolder holder;
     Camera camera;
     public CameraView(Context context) 
     {
      super(context);
      holder=getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
     }
     public void surfaceChanged(SurfaceHolder holder, int format,
       int width,int height) 
     {
    
      Camera.Parameters parameters=camera.getParameters();
      parameters.set("flash-mode", "on");
      camera.setParameters(parameters);
      camera.startPreview();
     }
     public void surfaceCreated(SurfaceHolder holder) 
     {
      try 
      {
       camera=Camera.open();
       camera.setPreviewDisplay(holder);
      } catch (IOException e) 
      {
       e.printStackTrace();
      }
     }
     public void surfaceDestroyed(SurfaceHolder holder) 
     {
      camera.stopPreview();
      camera.setPreviewCallback(null);
      camera.release();
      camera=null;
     }
    }
    

  2. Now Create front interface for camera Activity

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import android.app.Activity;
    import android.content.Intent;
    import android.hardware.Camera;
    import android.hardware.Camera.PictureCallback;
    import android.hardware.Camera.ShutterCallback;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.widget.*;
    
    public class CameraFront extends Activity
    {
     CameraView camview;
     FrameLayout frame;
     Button Click,Close;
     static String filename; 
     public void onCreate(Bundle bundle)
     {
      super.onCreate(bundle);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.camframe);
      Click=(Button)findViewById(R.id.buttonClick); 
      Click.setOnClickListener(new Button.OnClickListener(){
    
      public void onClick(View v) {
        
       camview.camera.takePicture(shutterCallback, 
         rawCallback, jpegCallback);
    
       }});
      Close=(Button)findViewById(R.id.buttonClose);
      Close.setOnClickListener(new Button.OnClickListener(){
    
      public void onClick(View v) {
      
       CameraFront.this.finish();
    
       }});
      camview=new CameraView(this);
      frame=(FrameLayout)findViewById(R.id.preview);
      frame.addView(camview);
     }
     ShutterCallback shutterCallback = new ShutterCallback() {
      public void onShutter() {
    
      }
     };
     PictureCallback rawCallback = new PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
    
      }
     };
     PictureCallback jpegCallback = new PictureCallback() {
      public void onPictureTaken(byte[] data, Camera camera) {
       FileOutputStream outStream = null;
      try {
    
       long d=System.currentTimeMillis();
       filename="/sdcard/"+d+".jpg";
    
       outStream = new FileOutputStream(
         String.format("/sdcard/%d.jpg", d));
       outStream.write(data);
       outStream.close();
    
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
    
      }
      CameraFront.this.finish();
      //ImageViewer to Show Image taken from camera
      Intent intent=new Intent(CameraFront.this,ImageViewer.class);
      intent.putExtra("imagefile", filename);
      startActivity(intent);
     }
    };
    }
    
    

  3. XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <FrameLayout
            android:id="@+id/preview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >
        </FrameLayout>
    
        <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:weightSum="1.0" >
    
            <Button
                android:id="@+id/buttonClick"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="0.5"
                android:text="Click" >
            </Button>
    
            <Button
               android:id="@+id/buttonClose"
               android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="0.5"
               android:text="Close" >
            </Button>
        </LinearLayout>
    
    </LinearLayout>
    
    

  4. It will store image in sdcard

Sunday, 14 October 2012

Install apk from assets android

  1. Copy/put your apk file in assets folder of your project
  2. Set onClick event of button to install apk. Copy and paste following code
  3. ===============================================
    public void loadMyApk()
    {
    copyAssets(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + "BS.apk")), "application/vnd.android.package-archive"); startActivityForResult(intent, 4);
    }
    ===============================================
    private void copyAssets()
    {
    AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { Log.e("tag", e.getMessage()); } for(String filename : files) { InputStream in = null; OutputStream out = null; try { //fileone=filename; in = assetManager.open(filename); out = new FileOutputStream("/sdcard/" + filename); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch(Exception e) { Log.e("tag", e.getMessage()); } }
    }
    ===============================================
    private void copyFile(InputStream in, OutputStream out) throws IOException
    {
    byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read); }
    }

    ===============================================
  4. Its done
In the above process I have copied apk file from assets folder to sdcard and installing from sdcard. If you want to delete from sdcard you can call delete() method for file

Wednesday, 29 August 2012

how to connect to internet on pc using android phone

[KEY : Tethering, Phone Internet, PC Internet]

Tethering mobile internet into PC / Laptop

Assuming that internet is activated on your phone
  1. Connect your android phone to pc
  2. Go to Settings
  3. Wireless and Network settings
  4. Tethering and portable hotspot
  5. USB tethering
Now at your pc
  1. Select "My Network Places"
  2. View Network Connections
here you will have your android phone as one more connection

Thursday, 26 July 2012

Java Hello World (First java program)

[KEY : HELLO WORLD, JAVA FIRST PROGRAM, RUN JAVA PROGRAM] Java Hello World program(first java program)
  1. Make sure class path has been set.(To set class path: Copy your bin folder location-> Right click on "My Computer" icon -> Select Advanced tab -> Click on Environment Variable button -> From System variable pane(lower pane) select path click on Edit button -> At variable value field put semicolon (;) and paste bin folder location and save )
  2. start notepad and copy the following code

    class Helloworld
    {
      public static void main(String str[])
      { 
        system.out.println("Hello World");
      }
    }
    

  3. Save file as Helloworld.java(same as your class name).
  4. Open CMD goto your folder where program is saved.
  5. Type javac Helloworld.java(it will compile you program).
  6. Now run with java Helloworld