Thursday, 21 May 2015

Null pointer exception after capturing image using android camera

Step 1).Open res->layout->activity_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"
   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="com.example.create_folder.MainActivity" >

   <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina" />

   <ImageButton
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerInParent="true" />

</RelativeLayout>


Step 2).Activity Class
  
package com.example.create_folder;

import  java.io.File;
import  java.util.Date;

import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import  android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity 
{
Button button;

ImageView image;

Uri uri;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


button= (Button) findViewById(R.id.button);
image = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new 
                           Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                                    
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img.jpg"); intent .putExtra(MediaStore.EXTRA_OUTPUTUri.fromFile(file));
     uri = Uri.fromFile(file);
startActivityForResult(intent 1);

}
});
}



protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1 && resultCode == RESULT_OK) {

super.onActivityResult(requestCode resultCode data);

BitmapFactory.Options options = new BitmapFactory.Options ();

options .inSampleSize = 8;

final Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(), options);

button.setVisibility(View.INVISIBLE);
image.setImageBitmap(bitmap);


}
}
}


Step 3).Add uses-permission  to your Manifest file


<uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Happy Coding :)