将照片从活动发送到另一个活动

时间:2015-04-14 08:28:32

标签: android android-camera

我需要拍照并发送MainActivity并将其发送到thirdActivity

这是MainActivity的代码

PS:我从ThirdActivity打开了主要活动

package com.fakecamera.hassanechafai.fakacamera;

import java.io.File;
import com.fakecamera.hassanechafai.fakacamera.R;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.fakecamera.hassanechafai.fakacamera.CameraPreview;
import com.fakecamera.hassanechafai.fakacamera.MyAlertDialogs;
import com.fakecamera.hassanechafai.fakacamera.NoCameraPresentDialog;

public class MainActivity extends Activity {
    private Camera mCamera;
    private CameraPreview mCameraPreview;
    public static Display display;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();

        mCamera = this.getCameraInstance();
        if (mCamera == null) {
            NoCameraPresentDialog noCamera = new NoCameraPresentDialog(
                    MainActivity.this);
            noCamera.showDialog();

        } else {
            mCameraPreview = new CameraPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
            preview.addView(mCameraPreview);

            ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
            captureButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCamera.takePicture(null, null, mPicture);
                    // mCamera.startPreview();
                }
            });
        }
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        Log.d("No of cameras", Camera.getNumberOfCameras() + "");
        for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
            CameraInfo camInfo = new CameraInfo();
            Camera.getCameraInfo(camNo, camInfo);
            if (camInfo.facing == (Camera.CameraInfo.CAMERA_FACING_FRONT)) {
                camera = Camera.open(camNo);
            }
        }
        return camera;
    }

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                notifyImageCapture(pictureFile.getPath());
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }
    };

    public void notifyImageCapture(String filepath) {
        Intent i = new Intent(this, ThirdActivity.class);
        i.putExtra("path", "" + filepath);
        startActivityForResult(i, 100);
    }

    public static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), "Photos");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("Photos", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
        Log.i("Mediapath", "" + mediaFile.getPath());
        return mediaFile;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 100) {
            this.finish();
            startActivity(new Intent(this, MainActivity.class));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            MyAlertDialogs dialogs = new MyAlertDialogs(MainActivity.this);
            dialogs.getRateMyAppDialog();
        }
        return super.onKeyDown(keyCode, event);
    }
}

这是我的第三个活动代码

package com.fakecamera.hassanechafai.fakacamera;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.os.Handler;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.graphics.Bitmap;
import com.fakecamera.hassanechafai.fakacamera.LoadImageBitmap;


public class ThirdActivity extends ActionBarActivity {
    private ImageView imageToEdit;
    private Bitmap orignalBitmap;
    private String CLICKED_IMAGE_FILE_PATH;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        ImageButton takeimage = (ImageButton)findViewById(R.id.imageButton3);

        takeimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ThirdActivity.this,
                        MainActivity.class);
                startActivity(intent);
            }
        });
        imageToEdit = (ImageView) this.findViewById(R.id.item_click_image);
      final  EditText edittext = (EditText)findViewById(R.id.editText);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    int time = Integer.parseInt(edittext.getText().toString());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent startActivity = new Intent(ThirdActivity.this, FullscreenActivity.class);
                            startActivity(startActivity);
                            finish();
                        }
                    }, time * 1000);
                }
            });
        CLICKED_IMAGE_FILE_PATH = getIntent().getStringExtra("path");
        this.setUpImage(CLICKED_IMAGE_FILE_PATH);
        }

    public void setUpImage(String path) {
        LoadImageBitmap imageLoader = new LoadImageBitmap();
        orignalBitmap = imageLoader.displayFullImage(path);
        if(CameraPreview.isPortrait){
            orignalBitmap = ImageEffects.rotate(imageLoader.displayFullImage(path));
        }else{
            orignalBitmap=imageLoader.displayFullImage(path);
        }
        imageToEdit.setImageBitmap(orignalBitmap);
    }
    @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_third, 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);
    }
}

我有任何错误,但当我尝试拍照时,我收到此消息: 不幸的是,FirstCamera已经停止了 我的问题是什么?

2 个答案:

答案 0 :(得分:1)

您应该从startActivityForResult致电ThirdActivity,然后在MainActivity中将照片设置为结果。为此,您需要确保照片模型实现Parcelable。 然后,您可以在setResult(RESULT_OK, Photo)中致电MainActivity,然后您会在方法ThirdActivity

中的onActivityResult中获取相片

答案 1 :(得分:0)

如果您的照片太大,您应该将其转换为字节数组以将其转换为Intent