Android 对使用相机在应用中拍照并发送额外 Intent 感到困惑

Android confused on using camera to take photo in app and send extra Intent

我正在尝试实现一项功能,如果有人拍照,它会用他们的照片替换最终页面源图像上的照片。我的演示程序运行良好,但我在实际程序中遇到了问题,我想在其中实现该功能。我正在学习本教程。我相信我的实际 classes 和构造函数开始变得比我现在理解的更复杂。 https://developer.android.com/training/camera/photobasics.html.

package com.example.samue.interactivefamilystory.main.ui;

import android.app.Activity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.media.Image; 
import android.provider.MediaStore; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle;
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageButton;

import com.example.samue.interactivefamilystory.R;

public class MainActivity extends AppCompatActivity {

    private EditText nameField;
    private Button startButton;
    private ImageButton cameraButton;

    private static final int REQUEST_IMAGE_CAPTURE = 1;
    Bitmap thumbnailImage = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameField = (EditText) findViewById(R.id.edit_name_text);
        startButton = (Button) findViewById(R.id.start_button);

        //Set button click listener and set name to use for resource.

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String name = nameField.getText().toString();
               startStory(name);
            }
        });

        cameraButton = (ImageButton) findViewById(R.id.camera_button);
        cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                takePictureIntent();

            }
        });

    }
    //start the story intent
    public void startStory(String name){
        Intent intent = new Intent(this, StoryActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("picture",thumbnailImage);
        startActivity(intent);

    }
    //take picture intent method.
    public void takePictureIntent(){
        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(takePicture.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePicture, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode== RESULT_OK) {
            Bundle extras = data.getExtras();
            thumbnailImage = (Bitmap) extras.get("data");
        }
    } 
}

故事活动

package com.example.samue.interactivefamilystory.main.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.samue.interactivefamilystory.R;
import com.example.samue.interactivefamilystory.main.model.Choice;
import com.example.samue.interactivefamilystory.main.model.Page;
import com.example.samue.interactivefamilystory.main.model.Story;


public class StoryActivity extends AppCompatActivity {

    private String name;
    private Bitmap usersPicture = null;

    private Story story;
    private ImageView storyImageView;
    private TextView storyTextView;
    private Button choice1button;
    private Button choice2button;

    public static final String TAG = StoryActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story);

        storyImageView = (ImageView) findViewById(R.id.story_image_view);
        storyTextView = (TextView) findViewById(R.id.story_text_view);
        choice1button = (Button) findViewById(R.id.next_page_button1);
        choice2button = (Button) findViewById(R.id.next_page_button2);



        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        if (name == null || name.isEmpty()){
            name = "Baby";
        }
        Log.d(TAG,name);

        usersPicture = intent.getExtras().getParcelable("picture");

        story = new Story();
        loadPage(0);


    }

    private void loadPage(int pageNumber) {
        final Page page = story.getPage(pageNumber);
        Drawable image = ContextCompat.getDrawable(this, page.getImageID());
        storyImageView.setImageDrawable(image);

        //get string from page then set the format special characters to name taken from edit text.
        String pageText = getString(page.getTextID());
        pageText = String.format(pageText, name);
        storyTextView.setText(pageText);


        if (page.isFinalPage()){
            if(usersPicture == null){
            storyImageView.setImageResource(R.drawable.baby);
            } else {

                storyImageView.setImageBitmap(usersPicture);
            }
            choice1button.setVisibility(View.INVISIBLE);
            choice2button.setText("I see...");

        }else {

            loadButtons(page);
        }

    }

    private void loadButtons(final Page page) {
        String buttonText = getString(page.getChoice1().getmTextID());
        buttonText = String.format(buttonText,name);
        choice1button.setText(buttonText);
        choice1button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int nextPage = page.getChoice1().getmNextPage();
                loadPage(nextPage);
            }
        });
        String buttonText2 = getString(page.getChoice2().getmTextID());
        buttonText2 = String.format(buttonText2, name);
        choice2button.setText(buttonText2);
        choice2button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int nextPage = page.getChoice2().getmNextPage();
                loadPage(nextPage);
            }

        });
    }
}

希望能提供帮助的更多信息。

嗯..让我重新开始我正在尝试实现一项功能,如果有人拍照,它会用他们的照片替换最后一页 src 图像上的照片。 据我所知,这句话应该设置图片,但它不是

if (page.isFinalPage()){
    if(usersPicture == null){
    storyImageView.setImageResource(R.drawable.baby);
    } else {

        storyImageView.setImageBitmap(usersPicture);
    }
    choice1button.setVisibility(View.INVISIBLE);
    choice2button.setText("I see...");

我的页面class

package com.example.samue.interactivefamilystory.main.model;

import android.widget.Button;

/**
 * Created by samue on 3/22/2018.
 */

public class Page {

    private int imageID;
    private int textID;

    private Choice choice1;
    private Choice choice2;

    private boolean isFinalPage = false;


    public Page(int imageID, int textID, Choice choice1, Choice choice2){

        this.imageID = imageID;
        this.textID = textID;
        this.choice1 = choice1;
        this.choice2 = choice2;

    }

    public Page(int imageID, int textID){
        this.imageID = imageID;
        this.textID = textID;
        isFinalPage = true;
    }

    public Page(int textID){
        this.textID = textID;
        isFinalPage = true;
    }


    public boolean isFinalPage() {
        return isFinalPage;
    }

    public void setFinalPage(boolean finalPage){
        isFinalPage = finalPage;
    }

    public int getImageID() {
        return imageID;
    }

    public void setImageID(int imageID) {
        this.imageID = imageID;
    }

    public int getTextID() {
        return textID;
    }

    public void setTextID(int textID) {
        this.textID = textID;
    }

    public Choice getChoice1() {
        return choice1;
    }

    public void setChoice1(Choice choice1) {
        this.choice1 = choice1;
    }

    public Choice getChoice2() {
        return choice2;
    }

    public void setChoice2(Choice choice2) {
        this.choice2 = choice2;
    }
}

和给我带来麻烦的页面

 pages[6] = new Page(R.drawable.baby, R.string.player);

我希望能为我所说的所有消息提供更多背景信息

您正在为回调中的两个值检查同一字段:

 if (requestCode == REQUEST_IMAGE_CAPTURE && requestCode == RESULT_OK)

您需要检查 RESULT_OK 的结果代码而不是请求代码:

 if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)

更新:

好的,你的错误是:

android.content.res.Resources$NotFoundException: Resource ID #0x0

说找不到这个:

Drawable image = ContextCompat.getDrawable(this, page.getImageID());

所以我会打印出 page.getImageID() 是 returning 的内容。它必须 return 您要加载的可绘制对象的 Android R 文件资源 ID。

我遇到的问题与您的类似,如果我没记错的话,您在获取 Drawable 时遇到了麻烦,所以,您最好按照以下步骤操作:

 public int getId(String resourceName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(resourceName);
            return idField.getInt(idField);
        } catch (Exception e) {
            throw new RuntimeException("No resource ID found for: "
                    + resourceName + " / " + c, e);
        }
    }

此方法 return 向您发送 id 您想要的内容,因此,您对该方法的调用应为:

Drawable image = ContextCompat.getDrawable(this, getId(page.getImageID(),R.drawable.class));

它将 return 您的 Drawable 的 ID,如果 page.getImageID() 与您要查找的图像 return 的名称相同,它将起作用,否则会崩溃。

例如,如果您有

R.drawable
   -image1
   -image2

你的 page.getImageID() 正在 return 发给你:image1,它会起作用。

编辑

要制作照片然后显示或保存到 Bitmap,您应该执行这些简单的步骤:

当你点击takePictureIntent()时,你必须创建这个Intent

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

然后要将其存储在 onActivityResult() 中,您必须这样做:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
            thumbnailImage = (Bitmap) data.getExtras().get("data"); 

        }  
    }