Java 安卓对在应用程序中使用摄像头拍照并发送额外意图感到困惑
我试图实现一个功能,如果有人拍了一张照片,它会用他们的图片替换最后一页源图像上的图片。我的演示程序运行得很好,但是我在想要实现该功能的实际程序中遇到了问题。我遵循这个教程。我相信我的实际类和构造函数开始变得比我现在理解的更复杂 故事活动Java 安卓对在应用程序中使用摄像头拍照并发送额外意图感到困惑,java,android,android-intent,android-camera-intent,Java,Android,Android Intent,Android Camera Intent,我试图实现一个功能,如果有人拍了一张照片,它会用他们的图片替换最后一页源图像上的图片。我的演示程序运行得很好,但是我在想要实现该功能的实际程序中遇到了问题。我遵循这个教程。我相信我的实际类和构造函数开始变得比我现在理解的更复杂 故事活动 package com.example.samue.interactivefamilystory.main.ui; import android.content.Intent; import android.graphics.Bitmap; import an
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...");
我的页面类
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)
您需要检查resultCode中的RESULT\u OK,而不是requestCode:
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返回的内容。它必须返回您要加载的可绘制文件的Android R文件资源id。我遇到了与您类似的问题,如果我没有错的话,您在获取可绘制文件时遇到了问题,因此,您最好遵循以下步骤:
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);
}
}
它将返回绘图表的id,如果page.getImageID返回的图像名称与您要查找的图像名称相同,则它将工作,否则它将崩溃
例如,如果你有
R.drawable
-image1
-image2
您的page.getImageID将返回给您:image1,它将工作
编辑
要制作照片并将其显示或保存到位图中,您应执行以下简单步骤:
当您单击TakePictureContent时,您必须创建此意图
然后要将其存储在onActivityResult中,您必须执行以下操作:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
thumbnailImage = (Bitmap) data.getExtras().get("data");
}
}
很抱歉,这是打字错误,但我仍然有问题,我添加了我的其他活动类。以及我得到的错误。@Bazz,对,但这个逻辑是在崩溃之后。所以它永远不会到达那里。第63行左右。添加一些println并看一看。谢谢,我想我更糊涂了。我相信位图不想作为src加载。我不确定我猜如何转换它,或者需要检查什么类型的语句。我可能需要从头开始,了解如何实现我想要的。重新表述我的问题,我正在努力学习如何更好地在线口述我的问题。不要从头开始,最好是调试它。你不可能总是从零开始做更大的项目。添加println或使用调试器:修复错误消息的格式,除非你想看看你被否决的速度有多快:哈哈,谢谢你提醒我,我其实只是在查找为什么我这么容易被否决。还在学习。你能解释一下在哪里撞车吗?或者,当它崩溃时,你想做什么?添加更多信息以尝试更好地解释。嗯。。在我尝试之前,我遇到的问题是显示玩家/用户启动应用程序时拍摄的图片,并将其显示在最终页面上。其他可绘制的页面显示得很好。所以,你的问题是显示从照片中拍摄的图像,对吗?是的,我不确定在浏览了多个页面后是否没有找到它,或者intent get extra没有正常工作。我相信我正在这样做,我遇到的问题是在我的故事应用程序的最后一页显示该图像。看看我在storyactivity类中的代码。我认为if语句与page类的构造函数相冲突。或者类似的东西。虽然你是在正确的轨道上,但当它尝试应用该图像时,有一些冲突。谢谢你,我将删除它。我不能说他们没有帮助,但这是你和DataDino所说的话和我更好地关注代码的结合。你的答案实际上是正确的,因为这就是我正在做的。有东西不对劲了。
R.drawable
-image1
-image2
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
thumbnailImage = (Bitmap) data.getExtras().get("data");
}
}