title: Captured image overwrites previous picture when i take picture after closing and reopening the app
body: <p>I have posted java code and the snapshot of the activity. The camera tab has a button and imagepreview widget. The camera is functioning properly. How to save the picture in a particular folder called pictures. And I'm not getting preview of the image.</p>
  
  <p>Here's my java code:</p>
  
  <pre><code>public class activities extends AppCompatActivity {
  
  public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
  ImageView imgPreview;
  Button btnCapturePicture;
  File file;
  static int count=0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activities);
  
      TabHost th=(TabHost)findViewById(R.id.tabHost);
      th.setup();
      TabHost.TabSpec specs=th.newTabSpec("Tag1");
      specs.setContent(R.id.camera);
      specs.setIndicator("",getResources().getDrawable(R.drawable.camera));
      th.addTab(specs);
  
      specs=th.newTabSpec("Tag2");
      specs.setContent(R.id.chat);
      specs.setIndicator("",getResources().getDrawable(R.drawable.chat15));
      th.addTab(specs);
  
      specs=th.newTabSpec("Tag3");
      specs.setContent(R.id.profile);
      specs.setIndicator("",getResources().getDrawable(R.drawable.ic_person_black_24dp));
      th.addTab(specs);
  
      specs=th.newTabSpec("Tag4");
      specs.setContent(R.id.history);
      specs.setIndicator("",getResources().getDrawable(R.drawable.history2));
      th.addTab(specs);
  
      specs=th.newTabSpec("Tag5");
      specs.setContent(R.id.settings);
      specs.setIndicator("",getResources().getDrawable(R.drawable.settings5));
      th.addTab(specs);
  
      imgPreview = (ImageView)findViewById(R.id.imageview6);
      btnCapturePicture=(Button)findViewById(R.id.btnCapturePicture);
  
      btnCapturePicture.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              //Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              file = new File(Environment.getExternalStorageDirectory()+ File.separator + "IMG_"+count+++".jpg");
              intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
              startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
          }
      });
  }
  
  
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
      //Check that request code matches ours:
      if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE)
      {
          //Get our saved file into a bitmap object:
          File file = new File(Environment.getExternalStorageDirectory()+File.separator + "IMG_"+count+++".jpg");
          Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
          imgPreview.setImageBitmap(bitmap);
  
      }
  }
  
  public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
  { // BEST QUALITY MATCH
  
      //First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, options);
  
      // Calculate inSampleSize, Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      options.inPreferredConfig = Bitmap.Config.RGB_565;
      int inSampleSize = 1;
  
      if (height &gt; reqHeight)
      {
          inSampleSize = Math.round((float)height / (float)reqHeight);
      }
      int expectedWidth = width / inSampleSize;
  
      if (expectedWidth &gt; reqWidth)
      {
          //if(Math.round((float)width / (float)reqWidth) &gt; inSampleSize) // If bigger SampSize..
          inSampleSize = Math.round((float)width / (float)reqWidth);
      }
  
      options.inSampleSize = inSampleSize;
  
      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
  
      return BitmapFactory.decodeFile(path, options);
  }
  }
  </code></pre>
  
  <p>Here's my xml layout:</p>
  
  <pre><code>&lt;LinearLayout
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:background="#eeeeee"
  android:orientation="vertical"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  &lt;android.support.design.widget.AppBarLayout
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
      &lt;!--&lt;android.support.v7.widget.Toolbar
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          android:layout_width="match_parent"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
          android:id="@+id/toolbar"/&gt;--&gt;
  &lt;/android.support.design.widget.AppBarLayout&gt;
  
  &lt;TabHost
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/tabHost"
      android:layout_gravity="center_horizontal"&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
          &lt;TabWidget
              android:id="@android:id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"&gt;
  
          &lt;/TabWidget&gt;
  
          &lt;FrameLayout
              android:id="@android:id/tabcontent"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
              &lt;LinearLayout
                  android:id="@+id/camera"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"&gt;
                  &lt;ImageView
                      android:layout_width="match_parent"
                      android:layout_height="350dp"
                      android:layout_gravity="center_horizontal"
                      android:layout_marginTop="50dp"
                      android:id="@+id/imageview6" /&gt;
  
                  &lt;Button
                      android:id="@+id/btnCapturePicture"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Take a Picture"
                      android:layout_gravity="center_horizontal"
                      android:layout_marginBottom="10dp" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/chat"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/profile"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"&gt;
                  &lt;ImageView
                      android:layout_width="150dp"
                      android:layout_height="150dp"
                      android:id="@+id/imageView3"
                      android:layout_gravity="center_horizontal"
                      android:layout_marginTop="46dp" /&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="40dp"
                      android:orientation="horizontal"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:weightSum="1"&gt;
                      &lt;TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:textSize="18sp"
                          android:text="Name"
                          android:id="@+id/textView13"
                          android:layout_gravity="center_vertical" /&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:gravity="end"
                          android:layout_marginStart="20dp"
                          android:layout_gravity="center_vertical"
                          android:id="@+id/textView18" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view1"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="40dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:orientation="horizontal"&gt;
                      &lt;TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Grade"
                          android:textSize="18sp"
                          android:id="@+id/textView14"
                          android:layout_gravity="center_vertical" /&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="New Text"
                          android:layout_marginStart="20dp"
                          android:layout_gravity="center_vertical"
                          android:gravity="end"
                          android:id="@+id/textView19" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view2"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="40dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:orientation="horizontal"&gt;
                      &lt;TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Credit Balance"
                          android:layout_gravity="center_vertical"
                          android:id="@+id/textView15"
                          android:textSize="18sp" /&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:text="New Text"
                          android:layout_gravity="center_vertical"
                          android:gravity="end"
                          android:layout_marginStart="20dp"
                          android:id="@+id/textView20" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view3"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="40dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:orientation="horizontal"&gt;
                      &lt;TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Invite Friends"
                          android:layout_gravity="center_vertical"
                          android:id="@+id/textView16"
                          android:textSize="18sp" /&gt;
                      &lt;ImageView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:id="@+id/imageView4"
                          android:src="@drawable/invite"
                          android:layout_marginStart="213dp"
                          android:layout_gravity="center_vertical"
                          android:foregroundGravity="right"
                          android:layout_marginEnd="3dp" /&gt;
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view4"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="40dp"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:orientation="horizontal"&gt;
                      &lt;TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Change Password"
                          android:id="@+id/textView17"
                          android:textSize="18sp"
                          android:layout_gravity="center_vertical" /&gt;
                      &lt;ImageView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:id="@+id/imageView5"
                          android:src="@drawable/change_passg"
                          android:layout_gravity="center_vertical"
                          android:layout_marginEnd="0dp"
                          android:layout_marginStart="170dp" /&gt;
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view5"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
  
  
  
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/history"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/settings"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="40dp"&gt;
                      &lt;View
                          android:layout_width="match_parent"
                          android:layout_height="2dp"
                          android:id="@+id/view6"
                          android:layout_marginLeft="10dp"
                          android:layout_marginRight="10dp"
                          android:background="#bdbdbd"&gt;
                      &lt;/View&gt;
                  &lt;/LinearLayout&gt;
  
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="50dp"&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_vertical"
                          android:gravity="center"
                          android:textSize="18sp"
                          android:text="Add Credits"
                          android:id="@+id/textView21" /&gt;
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view7"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="50dp"&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_vertical"
                          android:gravity="center"
                          android:textSize="18sp"
                          android:text="Report Issue"
                          android:id="@+id/textView22" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view8"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="50dp"&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_vertical"
                          android:gravity="center"
                          android:text="Rate Us"
                          android:textSize="18sp"
                          android:id="@+id/textView23" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view9"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="50dp"&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_vertical"
                          android:gravity="center"
                          android:textSize="18sp"
                          android:text="FAQ"
                          android:id="@+id/textView24" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view10"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
                  &lt;LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="50dp"&gt;
                      &lt;TextView
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_vertical"
                          android:gravity="center"
                          android:text="Feedback"
                          android:textSize="18sp"
                          android:id="@+id/textView25" /&gt;
  
                  &lt;/LinearLayout&gt;
                  &lt;View
                      android:layout_width="match_parent"
                      android:layout_height="2dp"
                      android:id="@+id/view11"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="10dp"
                      android:background="#bdbdbd"&gt;
                  &lt;/View&gt;
  
                  &lt;Button
                      android:layout_width="150dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_horizontal"
                      android:layout_marginTop="20dp"
                      android:text="Logout"
                      android:textColor="#FFFFFF"
                      android:background="@color/colorPrimary"
                      android:textSize="18sp"
                      android:id="@+id/button6" /&gt;
  
  
              &lt;/LinearLayout&gt;
          &lt;/FrameLayout&gt;
      &lt;/RelativeLayout&gt;
  &lt;/TabHost&gt;
  </code></pre>
  
  <p></p>
  
  <p>screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/B4RHU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B4RHU.jpg" alt="enter image description here"></a></p>