title: Using Bitmap for saving image to Gallery in Android application with camera app
body: <p>I have problems when i use some code from <a href="https://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity">Capture Image from Camera and Display in Activity</a> and <a href="https://developer.android.com/training/camera/photobasics.html" rel="nofollow noreferrer">https://developer.android.com/training/camera/photobasics.html</a></p>
  
  <p>First, this is in MainActivity.java. I create onClickevent when user press button "snap"</p>
  
  <pre><code> Button snap = (Button) findViewById(R.id.button3);
      snap.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent start_cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              File path = null;
              try {
                  path = createImageFile();
              }
              catch(Exception e)
              {
                  Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
              }
              if(path!=null) {
                  start_cam.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(path));
                  startActivityForResult(start_cam, 1);
              }
  
          }
  
      });
  </code></pre>
  
  <p>Next, i add createImageFile() method</p>
  
  <pre><code>private File createImageFile() throws IOException {
      // Create an image file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      String imageFileName = "JPEG_" + timeStamp + "_";
      File storageDir = Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES);
      File image = File.createTempFile(
              imageFileName,  // prefix
              ".jpg",         // suffix
              storageDir      // directory
      );
  
      // Save a file: path for use with ACTION_VIEW intents
      root_file = "file:" + image.getAbsolutePath();
      return image;
  }
  </code></pre>
  
  <p>Last part, I add onActivityResult()</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == 1 &amp;&amp; resultCode == RESULT_OK) {
          try {
              m = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(root_file));
              try {
                  view.setImageBitmap(m);
              }
              catch(Exception e)
              {
                  Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
  
              }
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I also add heading in my AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-feature android:name="android.hardware.camera" /&gt;
  &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;
  &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>After, i use this feature in android emulator API19 Nexus_one API19 (take a photo). The dialog appears and said "java.lang.nullPointerException".</p>
  
  <p>I don't know why this problem appears. I think it may be my emulator or code.</p>