Android 安卓打算用图片打开新屏幕

Android 安卓打算用图片打开新屏幕,android,Android,我有一个应用程序,目前使用相机拍摄图像,一旦拍摄图像,它会保存到SD卡上的文件夹中,然后同一屏幕保持打开,以便可以拍摄另一张照片 我希望它,所以当图像已采取和保存一个新的屏幕打开与新的屏幕上的图片 我已经创建了一个新的xml文件punch.xml和javapunch.java,并将它们注册到清单中。我只是不知道如何设置应用程序,以打开一个新屏幕,显示刚刚拍摄的图片 我猜这与保存图像后打开一个新的Intent有关 更新 好的,现在所有工作都开始执行,以便在下一个屏幕上显示图像吗? Punch.ja

我有一个应用程序,目前使用相机拍摄图像,一旦拍摄图像,它会保存到SD卡上的文件夹中,然后同一屏幕保持打开,以便可以拍摄另一张照片

我希望它,所以当图像已采取和保存一个新的屏幕打开与新的屏幕上的图片

我已经创建了一个新的xml文件
punch.xml
和java
punch.java
,并将它们注册到清单中。我只是不知道如何设置应用程序,以打开一个新屏幕,显示刚刚拍摄的图片

我猜这与保存图像后打开一个新的
Intent
有关

更新

好的,现在所有工作都开始执行,以便在下一个屏幕上显示图像吗?

Punch.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.punch);

    String myRef = this.getIntent().getStringExtra("filepath");
}
AndroidCamera.java更新以反映代码更改

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

final int RESULT_SAVEIMAGE = 0;

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

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl 
        = new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

    Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            camera.takePicture(myShutterCallback, 
                    myPictureCallback_RAW, myPictureCallback_JPG);


        }});
}

ShutterCallback myShutterCallback = new ShutterCallback(){

    public void onShutter() {
        // TODO Auto-generated method stub

    }};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub

    }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */
        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }

        Uri uriSavedImage = Uri.fromFile(output);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this, 
                    "Image saved", 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent intent = new Intent(getBaseContext(), Punch.class);
        intent.putExtra("filepath",uriSavedImage);
        //just using a request code of zero
        int request=0;
        startActivityForResult(intent,request); 
    }};

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(previewing){
        camera.stopPreview();
        previewing = false;
    }

    if (camera != null){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

    camera = Camera.open();
    try {
           Camera.Parameters parameters = camera.getParameters();
           if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
              // This is an undocumented although widely known feature
              parameters.set("orientation", "portrait");
              // For Android 2.2 and above
              camera.setDisplayOrientation(90);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(90);
           } else {
              // This is an undocumented although widely known feature
              parameters.set("orientation", "landscape");
              // For Android 2.2 and above
              camera.setDisplayOrientation(0);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(0);
           }
          camera.setParameters(parameters);
          camera.setPreviewDisplay(holder);
      } catch (IOException exception) {
         camera.release();

       }
        camera.startPreview();

    }


public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
}
}
公共类AndroidCamera扩展活动实现SurfaceHolder.Callback{
摄像机;
表面视图表面视图;
浮雕浮雕;
布尔预览=假;
LayoutFlater控件充气器=空;
最终整数结果_SAVEIMAGE=0;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView=(surfaceView)findViewById(R.id.camerapreview);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(surfaceHolder.SURFACE\u TYPE\u PUSH\u缓冲区);
controlInflater=LayoutInflater.from(getBaseContext());
视图控件=控件充气器。充气(R.layout.control,null);
LayoutParams LayoutParams控件
=新的LayoutParams(LayoutParams.FILL\u父级,
LayoutParams.FILL\u PARENT);
this.addContentView(viewControl、layoutParamsControl);
Button buttonTakePicture=(按钮)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(新建Button.OnClickListener()){
公共void onClick(视图arg0){
//TODO自动生成的方法存根
拍照(myShutterCallback,
myPictureCallback_RAW,myPictureCallback_JPG);
}});
}
ShutterCallback myShutterCallback=新建ShutterCallback(){
公共空间{
//TODO自动生成的方法存根
}};
PictureCallback myPictureCallback_RAW=新建PictureCallback(){
PictureTaken上的公共无效(字节[]arg0,摄像头arg1){
//TODO自动生成的方法存根
}};
PictureCallback myPictureCallback_JPG=新建PictureCallback(){
PictureTaken上的公共无效(字节[]arg0,摄像头arg1){
//TODO自动生成的方法存根
/*位图位图图片
=位图工厂.decodeByteArray(arg0,0,arg0.length)*/
int imageNum=0;
Intent-imageIntent=newintent(android.provider.MediaStore.ACTION\u-IMAGE\u-CAPTURE);
File imagesFolder=新文件(Environment.getExternalStorageDirectory(),“Punch”);

imagesFolder.mkdirs();//您可以保存对图像保存位置的引用,将其放在打开新活动的意图中。启动新活动并显示图像,允许用户删除等

Intent intent = new Intent(getBaseContext(), target.class);
intent.putExtra("ImageReference", reference);
startActivity(intent);

而target.class是活动类的名称。

您可以保存对图像保存位置的引用,将其放在打开新活动的意图中。启动新活动并显示图像,允许用户删除等

Intent intent = new Intent(getBaseContext(), target.class);
intent.putExtra("ImageReference", reference);
startActivity(intent);

而target.class是活动类的名称。

因此,在上面的代码中,您似乎要设置意图罚款。这将在拍照后立即运行

显然,您需要引用保存文件的位置。然后在新的活动类中,您调用Punch.class,您需要执行以下操作:-

String myRef = this.getIntent().getStringExtra("ImageReference");

这意味着您将字符串文件路径从主活动传递到您创建的新Punch.class。在这里,您可以使用文件路径打开图像。

因此,在上面的代码中,您似乎要设置意图精细。这将在拍照后立即运行

显然,您需要引用保存文件的位置。然后在新的活动类中,您调用Punch.class,您需要执行以下操作:-

String myRef = this.getIntent().getStringExtra("ImageReference");

这意味着您将字符串文件路径从主活动传递到您创建的新Punch.class。在这里,您可以使用文件路径打开映像。

这里的问题是您正在调用

camera.startPreview();
在你的pictureTaken回调中

将其替换为onClick中的代码,并将其更改为startActivityForResult():

然后可以实现onActivityResult并从那里调用
camera.startView()


别忘了在打孔活动中设置结果

这里的问题是您正在呼叫

camera.startPreview();
在你的pictureTaken回调中

将其替换为onClick中的代码,并将其更改为startActivityForResult():

然后可以实现onActivityResult并从那里调用
camera.startView()


别忘了在打孔活动中设置结果,这很有意义,但我如何才能将其添加到我的代码中,使其在拍照后自动转到带有图片的新屏幕?我认为您可以将其添加到onclick方法中,以便在拍照时、保存文件后等。然后以任何形式获取对文件的引用将引用存储在中,并将其发送到新活动。这是有道理的,但我如何将其添加到代码中,以便在拍摄后自动转到带有图片的新屏幕?我认为您可以将其添加到onclick方法中,以便在拍摄照片时、保存文件后等。然后在中获取对文件的引用不管是什么格式