如何将多个图像发送到android中的另一个活动?

时间:2014-05-19 10:40:20

标签: android android-camera android-imageview

在我的应用程序中,我创建了一个自定义相机,它可以捕获图像并将其保存在SD卡中。并且还可以在第二个活动中查看它。但它只是取代了我以前不想要的图像。如何将多个图像发送到第二个活动?

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.surfaceview);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        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.custom, null);
        LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);
        click = (Button) findViewById(R.id.button_capture);
        click.setOnClickListener(new OnClickListener(){

             public void onClick(View arg0) {
                   // TODO Auto-generated method stub

                   camera.takePicture(myShutterCallback,
                     myPictureCallback_RAW, myPictureCallback_JPG);
                  }});
                  }

                  ShutterCallback myShutterCallback = new ShutterCallback(){

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

                 }};

                PictureCallback myPictureCallback_RAW = new PictureCallback(){

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

                 }};

                PictureCallback myPictureCallback_JPG = new PictureCallback(){

                 @Override
                 public void onPictureTaken(byte[] arg0, Camera arg1) {
                  // TODO Auto-generated method stub
                  Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);


                  filepath = Environment.getExternalStorageDirectory()+"/testing/"+"test.3gp";
                    System.out.println("thumbnail path~~~~~~"+filepath);
                    File file = new File(filepath);
                    Uri uriTarget = Uri.fromFile(file);

                  OutputStream imageFileOS;
                  try {
                   imageFileOS = getContentResolver().openOutputStream(uriTarget);
                   imageFileOS.write(arg0);
                   imageFileOS.flush();
                   imageFileOS.close();
                   Toast.makeText(CustomCameraActivity.this, "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();
                   Intent returnIntent = new Intent();
                   returnIntent.putExtra("result",filepath.toString());
                   setResult(1,returnIntent);     
                   finish();
                  } catch (FileNotFoundException e) {

                   // TODO Auto-generated catch block

                   e.printStackTrace();

                  } catch (IOException e) {

                   // TODO Auto-generated catch block

                   e.printStackTrace();

                  }
                 // camera.startPreview();

                 }};

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

}

1 个答案:

答案 0 :(得分:1)

只需将您的图片转换为位图,然后通过Bitmap

将该yourIntent.putExra("key",bitmap);传递到另一项活动中

或者使用它:

 private File imageFile;
 private Uri imageUri;
 private static final int CAMERA_REQUEST_CODE = 100;
 private String imagePath
 private Uri getTempUri() {
        // Create an image file name
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String dt = sdf.format(new Date());
        imageFile = null;
        imageFile = new File(Environment.getExternalStorageDirectory()
                + "/foldername/", "Camera_" + dt + ".jpg");
        Applog.Log(
                TAG,
                "New Camera Image Path:- "
                        + Environment.getExternalStorageDirectory()
                        + "/foldername/" + "Camera_" + dt + ".jpg");
        File file = new File(Environment.getExternalStorageDirectory()
                + "/foldername");
        if (!file.exists()) {
            file.mkdir();
        }
        if (!imageFile.exists()) {
            try {
                imageFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        imagePath = Environment.getExternalStorageDirectory() + "/foldername/"
                + "Camera_" + dt + ".jpg";
        imageUri = Uri.fromFile(imageFile);
        return imageUri;
    }

现在当你启动相机激活时。

    Intent cameraIntent = new Intent(
                                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                                cameraIntent.putExtra(
                                        MediaStore.EXTRA_OUTPUT,
                                        getTempUri());
                                startActivityForResult(cameraIntent,
                                        CAMERA_REQUEST_CODE);

现在在ActivityResult中:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (CAMERA_REQUEST_CODE == requestCode && resultCode == RESULT_OK) {

        String yourUri=ImagePath;
            //now store this Uri it will contain capture image url.
        }
}