-1

I am getting FATAL EXCEPTION while trying to draw image using ImageView after taking that image using mobile camera.But i am getting exception while creating image using Bitmap.

Code::

   public class MMS extends Activity implements OnClickListener {
    int TAKE_PHOTO_CODE = 0;
    public static int count=0;
    EditText preLoc,comeby;
Button ok,capture;
String photo;
String dir;

boolean GPS,flag;
String cityName=null; 
String SubThorugh = null;
Intent i;
ImageView iv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mms);

    preLoc = (EditText)findViewById(R.id.etPreLoc1);
    comeby = (EditText)findViewById(R.id.etComing1);
    iv = (ImageView)findViewById(R.id.ImageView1);
    //ok = (Button)findViewById(R.id.bOK1);
    capture = (Button) findViewById(R.id.btnCapture);
    capture.setOnClickListener(this);

}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId())
    {
    case R.id.btnCapture:
        capturePicture();
        break;

    }
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Log.d("CameraDemo", "Pic saved");
        Toast.makeText(getApplicationContext(), "photo saved as: "+photo, Toast.LENGTH_LONG).show();
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(photo);
    }
}
private void capturePicture() {

    //here,we are making a folder named picFolder to store pics taken by the camera using this application
    final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; 
    File newdir = new File(dir); 
    newdir.mkdirs();
 // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
    count++;
    String file = dir+count+".jpg";
    photo = file;
    File newfile = new File(file);
    try {
        newfile.createNewFile();
    } catch (IOException e) {}       

    Uri outputFileUri = Uri.fromFile(newfile);

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

}

}

and Exception was :

06-04 22:15:46.515: W/dalvikvm(22711): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-04 22:15:46.546: E/AndroidRuntime(22711): FATAL EXCEPTION: main
06-04 22:15:46.546: E/AndroidRuntime(22711): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.MMS}: java.lang.NullPointerException
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.os.Looper.loop(Looper.java:130)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at java.lang.reflect.Method.invoke(Method.java:507)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at dalvik.system.NativeStart.main(Native Method)
06-04 22:15:46.546: E/AndroidRuntime(22711): Caused by: java.lang.NullPointerException
06-04 22:15:46.546: E/AndroidRuntime(22711):    at com.example.nirbhaya.MMS.onActivityResult(MMS.java:125)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-04 22:15:46.546: E/AndroidRuntime(22711):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-04 22:15:46.546: E/AndroidRuntime(22711):    ... 11 more
06-04 22:15:46.921: D/dalvikvm(22711): GC_CONCURRENT freed 268K, 48% free 2956K/5639K, external 583K/945K, paused 3ms+4ms

code at line no 125 is Bitmap photo = (Bitmap) data.getExtras().get("data");

Clay
  • 4,700
  • 3
  • 33
  • 49
KCRaju
  • 544
  • 3
  • 7
  • 21

2 Answers2

0

Quoting the MediaStore documentation:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

That implies to me that if you have set the EXTRA_OUTPUT parameter, it's not going to return a Bitmap object in the extra field - it's only going to write the full-sized image to the specified Uri.

James Holderness
  • 22,721
  • 2
  • 40
  • 52
0

I agree with the James's answer that's correct so you have to pick the image directly from the path where you stored it in your case that path would be

String path=newfile.getAbsolutePath(); and then you can create the bitmap from that path using this function which takes path as parameter and returns bitmap .

public Bitmap getImage1(String path) throws IOException
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        int[] newWH =  new int[2];
        newWH[0] = srcWidth/2;
        newWH[1] = (newWH[0]*srcHeight)/srcWidth;

        int inSampleSize = 1;
        while(srcWidth / 2 >= newWH[0]){
            srcWidth /= 2;
            srcHeight /= 2;
            inSampleSize *= 2;
        }

         options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inSampleSize = inSampleSize;
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
        ExifInterface exif = new ExifInterface(path);
        String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
        Matrix matrix = new Matrix();
        float rotation = rotationForImage(con, Uri.fromFile(new File(path)));
        if (rotation != 0f) {
            matrix.preRotate(rotation);
        }

        Bitmap pqr=Bitmap.createBitmap(
                sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);


        return pqr;
    }   


    public  float rotationForImage(Context context, Uri uri) {
        if (uri.getScheme().equals("content")) {
            String[] projection = { Images.ImageColumns.ORIENTATION };
            Cursor c = context.getContentResolver().query(
                    uri, projection, null, null, null);
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } else if (uri.getScheme().equals("file")) {
            try {
                ExifInterface exif = new ExifInterface(uri.getPath());
                int rotation = (int)exifOrientationToDegrees(
                        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL));
                return rotation;
            } catch (IOException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
             }

        }
        return 0f;
    }

    private static float exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }
}

other functions are used to give the correct orientation for the image.

Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37