Android Studio-拍摄照片并将其保存在新活动中

Android Studio-拍摄照片并将其保存在新活动中,android,android-studio,Android,Android Studio,我已经拍了一张活动照,可以捕捉图像,然后将其存储在我的手机图库中。我现在的问题是,我想将应用程序拍摄的每张照片保存在一个名为“Gallery”的新android活动中,但我不知道如何保存 以下是拍照的代码: import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.

我已经拍了一张活动照,可以捕捉图像,然后将其存储在我的手机图库中。我现在的问题是,我想将应用程序拍摄的每张照片保存在一个名为“Gallery”的新android活动中,但我不知道如何保存

以下是拍照的代码:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class TakePicture extends ActionBarActivity implements OnClickListener {
    //keep track of camera capture intent
    final int CAMERA_CAPTURE = 1;
    //captured picture uri
    private Uri picUri;
    final int PIC_CROP = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Button btn6 = (Button)findViewById(R.id.button240);

        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TakePicture.this, YesTracker.class));
            }
        });


        Button btn8 = (Button)findViewById(R.id.button241);

        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TakePicture.this, Home.class));
            }
        });

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_picture);
        //retrieve a reference to the UI button
        Button captureBtn = (Button)findViewById(R.id.capture_btn);
        //handle button clicks
        captureBtn.setOnClickListener(this);
    }
    public void onClick(View v) {
        if (v.getId() == R.id.capture_btn) {
            try {
                //use standard intent to capture an image
                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //we will handle the returned data in onActivityResult
                startActivityForResult(captureIntent, CAMERA_CAPTURE);
            } catch(ActivityNotFoundException anfe){
                //display an error message
                String errorMessage = "Whoops - your device doesn't support capturing images!";
                Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(requestCode == CAMERA_CAPTURE){
                picUri = data.getData();
                performCrop();
            } //user is returning from cropping the image
            else if(requestCode == PIC_CROP){
                //get the returned data
                Bundle extras = data.getExtras();
                //get the cropped bitmap
                Bitmap thePic = extras.getParcelable("data");
                //retrieve a reference to the ImageView
                ImageView picView = (ImageView)findViewById(R.id.picture);
                //display the returned cropped image
                picView.setImageBitmap(thePic);
            }
        }
    }
    private void performCrop(){
        try {
            //call the standard crop action intent (the user device may not support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            //indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            //set crop properties
            cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            //retrieve data on return
            cropIntent.putExtra("return-data", true);
            //start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        }
        catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_take_picture, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


你知道我该怎么做吗?

你不能在
活动中“保存”任何东西,
活动是一个显示数据并允许用户与你的应用程序交互的屏幕-尽管这是一个非常粗略的定义。

但您可以将照片的路径存储在SQLite数据库或JSON文件中。因此,当您打开“多媒体资料”活动时,您可以通过读取存储器中的路径并对该路径执行所需的逻辑来显示照片。

您可以提供照片将存储到的路径。开发者网站上也有关于这方面的教程。看看这里-
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BACK"
    android:id="@+id/button240"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HOME"
    android:id="@+id/button241"
    android:layout_alignBottom="@+id/button240"
    android:layout_alignParentEnd="true" />

<Button
    android:id="@+id/capture_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capture"
    android:layout_marginTop="77dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:id="@+id/picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/picture"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/pic_border" />