摄像头拍摄的图像应转发到android中的下一个屏幕

时间:2011-02-02 08:31:23

标签: android camera

这是我的代码

package com.marakana;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class CameraActivity extends Activity {
  private static final String TAG = "CameraDemo";
  Preview preview; // <1>
  Button buttonClick; // <2>
  myglobal m=new myglobal();
  String aa;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    preview = new Preview(this); // <3>
    ((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>

    buttonClick = (Button) findViewById(R.id.buttonClick);
    buttonClick.setOnClickListener(new OnClickListener() {
      public void onClick(View v) { // <5>
        preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
      }
    });

    Log.d(TAG, "onCreate'd");
  }

  // Called when shutter is opened
  ShutterCallback shutterCallback = new ShutterCallback() { // <6>
    public void onShutter() {
      Log.d(TAG, "onShutter'd");
    }
  };

  // Handles data for raw picture
  PictureCallback rawCallback = new PictureCallback() { // <7>
    public void onPictureTaken(byte[] data, Camera camera) {
      Log.d(TAG, "onPictureTaken - raw");
    }
  };

  // Handles data for jpeg picture
  PictureCallback jpegCallback = new PictureCallback() { // <8>
    public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
      try {
        // Write to SD Card


          outStream = new FileOutputStream(String.format("/sdcard/DCIM/%d.jpg",
            System.currentTimeMillis()));

          m.setAa(aa);
         Toast.makeText(getBaseContext(),aa,1000).show();
        outStream.write(data);
        outStream.close();
        Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
        Intent i=new Intent();
        i.setClassName("com.marakana", "com.marakana.image");
        startActivity(i);
      } catch (FileNotFoundException e) { // <10>
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
      }
      Log.d(TAG, "onPictureTaken - jpeg");
    }
  };

}

2 个答案:

答案 0 :(得分:2)

我认为你发送下一个活动的路径会更好。所以你可以从那个活动中读到它。

String path =  String.format("/sdcard/DCIM/%d.jpg",System.currentTimeMillis());
outStream = new FileOutputStream(path);
//other codes 
Intent i=new Intent();
i.setClassName("com.marakana", "com.marakana.image");
i.putExtra("path", path);
startActivity(i);

因此,您可以通过

访问该路径
String path = getIntent().getExtra("path");
//so you can create drawable or bitmap using this path

答案 1 :(得分:0)

i.putExtra("img", data);

然后在另一个活动中使用

getIntent().getExtra("img");