0

Just wondering if anyone could help me figure this out as Im a major newbie with java. What I would like to do is to take a photo when i press a button and have it display in a new activity after. What I have got working so far is when i click the button the camera opens up takes a photo saves it to my gallery and then takes me back to the same activity. Any help is appreciated!

This is my mainactivity.java file

package com.example.appname;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends Activity {

private File mFileUri;
private final Context mContext = this;

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

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    mFileUri = getOutputMediaFile(1);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);

    // start the image capture Intent
    startActivityForResult(intent, 100);
  }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    if (resultCode == RESULT_OK) {
        if (mFileUri != null) {
            String mFilePath = mFileUri.toString();
            if (mFilePath != null) {
                Intent intent = new Intent(mContext, CamActivity.class);
                intent.putExtra("filepath", mFilePath);
                startActivity(intent);



            }
        }
     }               
 }


 // Return image / video
 private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new   File(Environment.getExternalStorageDirectory(), "DCIM/Camera");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == 1) { // image
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == 2) { // video
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
 }
}

This is my activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#000"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.appname.MainActivity" >

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:minHeight="50dp"
    android:minWidth="120dp"
    android:onClick="@id/activity_cam"
    android:text="@string/LetsBegin" />

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp"
    android:src="@drawable/abc_ab_share_pack_mtrl_alpha" />

 </RelativeLayout>

This is my CamActivity.java which has nothing on it but an image view and it is where i would like the image to show after the camera saves the photo

package com.example.appname;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

import java.io.File;

public class CamActivity extends Activity {

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

    Intent intent = getIntent();
    String filepath = intent.getStringExtra("filepath");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8; // down sizing image as it throws OutOfMemory  Exception for larger images
    filepath = filepath.replace("file://", ""); // remove to avoid  BitmapFactory.decodeFile return null
    File imgFile = new File(filepath);
    if (imgFile.exists()) {
        ImageView imageView = (ImageView) findViewById(R.id.imgphoto);
        Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),  options);
        imageView.setImageBitmap(bitmap);
      }
   }


}

My activity_cam.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_cam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.isyourfriendaterrorist.CamActivity" >

<ImageView
    android:id="@+id/imgphoto"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/imageview"/>

</RelativeLayout>
Essentialz
  • 45
  • 3
  • 13

1 Answers1

2

You should have the following:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if (resultCode == RESULT_OK) {
            if (mFileUri != null) {
                String mFilePath = mFileUri.toString();
                if (mFilePath != null) {
                    Intent intent = new Intent(mContext, SecondActivity.class);
                    intent.putExtra("filepath", mFilePath);
                    startActivity(intent);
                }
            }
        }               
    }

Please read my answer with sample code at the following question (I use Android Studio, not Eclipse, however I think it's easy for you to understand):

How to move image captured with the phone camera from one activity to an image view in another activity?

Hope this helps!

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
  • hey i looked at your answer in the other example and changed my answer completly if you look at the code above. Now it opens up the camera, takes a photo, saves to SD card and goes to new activity. But it doesnt display the picture in the image view:( Please help! – Essentialz Nov 09 '15 at 21:35
  • Pls check if your project has many `activity_cam.xml` files or not, if yes, check their contents, if no, have you try debugging the app? Moreover, try create a new empty project with all my source codes from `UPDATE WIH FULL SOURCE CODE (NEW PROJECT):` to `END OF NEW PROJECT` – BNK Nov 09 '15 at 22:04
  • hey i got it working now!:) but now another issue came up:( lol when i open up the app and take a pic via portrait mode it always displays horiztonally. If i open up the camera while holding the phone in portrait mode then rotating phone to horizontal and take a picture nothing happens it clicks but it doesnt display the picture however if i open up the app holding the phone in landscape mode and take photo it saves and displays the pic properly. Any idea how to fix this? Thank you again:) – Essentialz Nov 11 '15 at 22:44