-3

I am trying to implement a feature if someone takes a picture it replaces the picture on the final page source image with theirs. my demo program worked fine, but I am having trouble with the actual program I want to implement the feature in. I am following this tutorial. I believe with my actual classes and constructors start complicating more then I understand right now. 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");
        }
    } 
}

StoryActivity

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

        });
    }
}

More information that hopefully help.

Hmm.. let me start over I am trying to implement a feature if someone takes a picture it replaces the picture on the final page src image with theirs. this statement with my knowledge should set the picture, but it isn't

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

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

my page 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;
    }
}

and page that is causing me trouble

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

I hope that gives more context to what I am saying sorry for all the messages

Bazz
  • 34
  • 8

2 Answers2

0

You are checking the same field for two values in your callback:

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

You need to check the resultCode for RESULT_OK not the requestCode:

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

Update:

OK so your error:

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

Says that it couldn't find this:

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

So I'd print out what page.getImageID() is returning. It must return the Android R file resource id of the drawable you want to load.

DataDino
  • 1,507
  • 1
  • 15
  • 30
  • Sorry that was typo, but I am still having trouble I added my other activity class. as well as error I am getting. – Bazz Mar 26 '18 at 00:20
  • @Bazz, right, but that logic is after the crash. So it will never get there. Line #63 or so. Add some `println` and take a look. – DataDino Mar 26 '18 at 00:46
  • Thanks, I think I confused myself more. I believe the bitmap doesn't want to load as the src. I am not sure I guess how to convert it or what kind of statement needs to be checked. I might have to just start from scratch on how to implement what I want. – Bazz Mar 26 '18 at 00:52
  • Rephrased my question, I am trying to learn how dictate my questions online better. – Bazz Mar 26 '18 at 01:09
  • 1
    Don't start over, it's better to debug it. You won't always be able to "start from scratch" on bigger projects. Add `println()` or use the debugger: https://developer.android.com/studio/debug/index.html – DataDino Mar 26 '18 at 01:11
  • I hear you, those are the next tutorials I am going to work on really using the debugger. I have a lot of trouble with it. – Bazz Mar 26 '18 at 01:17
0

I had a problem similar than yours, if I'm not wrong you are having troubles getting the Drawable so, you maybe better follow this :

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

This method is returning to you the id what you want, so, your call to this method should be :

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

It will return the id of your Drawable, it will work if the page.getImageID() is returning the same name of that image that you are looking for, otherwise it will crash.

For example, if you have

R.drawable
   -image1
   -image2

And your page.getImageID() is returning to you : image1, it will work.

EDIT

To make a photo and then displaying it or saving into Bitmap you should do those simple steps :

When you click on takePictureIntent(), you have to create this Intent

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

Then to store it, in onActivityResult() you have to do this :

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

        }  
    } 
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Hmm.. before I try this the thing I am having trouble with is displaying the picture the player/user takes when they start the app and displaying it on the final page. The other pages drawable display fine though. – Bazz Mar 26 '18 at 01:15
  • So, your problem is displaying the image taken from photo, right? – Skizo-ozᴉʞS ツ Mar 26 '18 at 01:16
  • Yeah I am not sure if maybe after going through multiple pages it is not finding it or that the intent get extra isn't working right. – Bazz Mar 26 '18 at 01:18
  • I believe I am doing that, where I am having trouble is displaying that image on the final page of my story app. Take a look at my code in the storyactivity class. I think my if statement is conflicting with my constructor of my page class. Or something along those lines. Though you are on the right track there is something conflicting when it tries to apply that image. – Bazz Mar 26 '18 at 01:27
  • I need to see your code, can you .zip it and upload it somewhere so I can check it out? – Skizo-ozᴉʞS ツ Mar 26 '18 at 01:28
  • I need to see that not here on StackOverflow, I need to study what you're trying to do, and then I'll can find what you are missing – Skizo-ozᴉʞS ツ Mar 26 '18 at 01:29
  • Thanks for helping me I literally have no idea what I just changed, but I was uploading to GitHub and did a new build really fast, after I just moved a line of code around and it worked. I call that I need a break, thank you for your help. Sometime I literally just need someone to talk to. – Bazz Mar 26 '18 at 01:36
  • Yeah I really think just talking things through with people just helps me and give me better clarity. – Bazz Mar 26 '18 at 01:38
  • Then if noone of answers helped to you, remove the question please :) otherwise mark any as a correct one – Skizo-ozᴉʞS ツ Mar 26 '18 at 01:39
  • 1
    Thank you, so much I will remove it. I can't say they didn't help, but it was a combination of what you and DataDino said and me just getting a better focus on the code. Your answer is actually correct, though because it was what I was doing. Something just got out of place. – Bazz Mar 26 '18 at 01:43