在关闭并重新打开应用程序后拍摄照片时,拍摄的图像会覆盖上一张照片

时间:2016-04-17 02:22:44

标签: android-tabhost android-xml

我已经发布了java代码和活动的快照。相机选项卡有一个按钮和imagepreview小部件。相机功能正常。如何将图片保存在名为图片的特定文件夹中。而且我没有预览图片。

这是我的java代码:

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 > reqHeight)
    {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth)
    {
        //if(Math.round((float)width / (float)reqWidth) > 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);
}
}

这是我的xml布局:

<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">
<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <!--<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"/>-->
</android.support.design.widget.AppBarLayout>

<TabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabHost"
    android:layout_gravity="center_horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/camera"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="350dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="50dp"
                    android:id="@+id/imageview6" />

                <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" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/chat"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/profile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <ImageView
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:id="@+id/imageView3"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="46dp" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:orientation="horizontal"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:weightSum="1">
                    <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" />
                    <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" />

                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal">
                    <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" />
                    <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" />

                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal">
                    <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" />
                    <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" />

                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal">
                    <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" />
                    <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" />
                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal">
                    <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" />
                    <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" />
                </LinearLayout>
                <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">
                </View>




            </LinearLayout>

            <LinearLayout
                android:id="@+id/history"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/settings"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="40dp">
                    <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">
                    </View>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                    <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" />
                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                    <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" />

                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                    <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" />

                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                    <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" />

                </LinearLayout>
                <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">
                </View>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp">
                    <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" />

                </LinearLayout>
                <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">
                </View>

                <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" />


            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>
</TabHost>

截图:

enter image description here

0 个答案:

没有答案