当前位置: 首页 > 知识库问答 >
问题:

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

湛铭
2023-03-14

我需要拍照并发送到主活动并发送到第三活动

这是MainActivity的代码

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);
    }
}

我有任何错误,但当我试图拍摄一张照片时,我得到这样的信息:不幸的是,第一相机停止了,我的问题是什么?

共有1个答案

冯鸿哲
2023-03-14

您应该从thirdactivity调用startactivityforresult,然后将照片设置为mainactivity中的结果。为此,您需要确保您的照片模型实现parcelable。然后,您可以在mainActivity中调用setResult(RESULT_OK,Photo),您将在OnActivityResult方法的ThirdActivity中返回照片

 类似资料:
  • 我试图将图像从一个活动发送到另一个活动,但我不知道如何设置ImageView。

  • 我目前正在做一个项目,我需要帮助。 我想发送的名称,总数量和总价的一个产品的数量在我的添加到购物车活动。 @override public void onListClick(final ItemInfo item){ 上面应该有什么代码发送到购物车活动?? toast.maketext(mainactivity.this,“added To cart”,toast.length_short).sh

  • 问题内容: 我对android非常陌生,我正在尝试将用户输入的数据(他们的名字)发送到另一个活动。过去,我可以使用Intent在活动之间发送单行代码,但是我无法解决如何向两个不同的TextView发送两个不同的字符串。 这是到目前为止我的MainActivity代码: 我第二项活动MainGame的代码: 当我运行它时,我得到了两个TextView中都为“ name2”添加的内容。我需要做些什么来

  • 我在活动2中有一个字符串 我想在activity1的文本字段中插入此字符串。我该怎么做?

  • 我需要将数据从片段发送到另一个活动 我在Homeactive下的LoadsFraank中使用此代码 在另一个活动(LoadActivity)中接收数据 但是意图没有附加条件 请看下面的截图

  • 问题内容: 我正在尝试将图像从一项活动发送到另一项活动,但我不知道如何设置imageview。 这是我发送图像和其他内容的方式 这是我尝试读取图像并将其设置为ImageView的方法,但是出现语法错误。 我应该如何设置ImageView? 问题答案: 你不觉得 应该