1

In my main activity, you click a button opens the Camera app. Once a photo is taken in the Camera app, you land on the second activity. So the button opens the camera app and the second activity.

I want the picture that was taken in the first activity to be shown in the second activity. However, right now the code has it showing up in the main activity.

This little android noob appreciates literally any help!

I used code for the camera function from: Capture Image from Camera and Display in Activity

main activity java (MainActivity.java):

    import android.Manifest;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
            private static final int CAMERA_REQUEST = 1888;
            private ImageView imageView;
            private static final int MY_CAMERA_PERMISSION_CODE = 100;
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                this.imageView = (ImageView) this.findViewById(R.id.takepicture);
                Button photoButton = (Button) this.findViewById(R.id.button3);
                photoButton.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, Glamme.class);
                        startActivity(intent);
                        if (checkSelfPermission(Manifest.permission.CAMERA)
                                != PackageManager.PERMISSION_GRANTED) {
                            requestPermissions(new String[]{Manifest.permission.CAMERA},
                                    MY_CAMERA_PERMISSION_CODE);
                        } else {
                            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(cameraIntent, CAMERA_REQUEST);
                        }
                    }
                });
            }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if (requestCode == MY_CAMERA_PERMISSION_CODE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                    Intent cameraIntent = new
                            Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                } else {
                    Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
                }
    
            }
        }
    
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    imageView.setImageBitmap(photo);
                    }
                }
    
            }

main activity xml (activity_main.xml):

 <Button
        android:id="@+id/button3"
        android:layout_width="98dp"
        android:layout_height="0dp"
        android:layout_marginBottom="1dp"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/glam_me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView6" />

    <ImageView
        android:id="@+id/takepicture"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:layout_marginEnd="67dp"
        android:layout_marginStart="67dp"
        android:contentDescription="@string/todo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:color/transparent" />

</android.support.constraint.ConstraintLayout>

second activity java (Glamme.java)

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Glamme extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glamme);
        this.imageView = (ImageView) this.findViewById(R.id.takepicture);

        Bitmap bitmap=null;
        Intent intent = getIntent();
        if(intent!=null)
            bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
        imageView.setImageBitmap(bitmap);
}

second activity (activity_glamme.xml):

<ImageView
        android:id="@+id/displaypicture"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="40dp"
        android:layout_marginEnd="67dp"
        android:layout_marginStart="67dp"
        android:contentDescription="@string/todo"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:srcCompat="@android:color/transparent" />

</android.support.constraint.ConstraintLayout>

Logs:

 I/zygote: Not late-enabling -Xcheck:jni (already on)
 W/zygote: Unexpected CPU variant for X86 using defaults: x86
 I/zygote: WaitForGcToComplete blocked Background on None for 7.876ms
 I/zygote: Background concurrent copying GC freed 8091(4MB) AllocSpace objects, 0(0B) LOS objects, 63% free, 897KB/2MB, paused 1.607ms total 367.085ms
 I/InstantRun: starting instant run server: is main process
 D/OpenGLRenderer: HWUI GL Pipeline
 I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
 I/OpenGLRenderer: Initialized EGL, version 1.4
 D/OpenGLRenderer: Swap behavior 1
 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
 D/OpenGLRenderer: Swap behavior 0
 D/EGL_emulation: eglCreateContext: 0xdc485240: maj 2 min 0 rcv 2
 D/EGL_emulation: eglMakeCurrent: 0xdc485240: ver 2 0 (tinfo 0xdc4832a0)
 D/EGL_emulation: eglMakeCurrent: 0xdc485240: ver 2 0 (tinfo 0xdc4832a0)
 V/StudioProfiler: StudioProfilers agent attached.
 V/StudioProfiler: Acquiring Application for Events
 V/StudioProfiler: Transformed class: java/net/URL
 W/zygote: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
 V/StudioProfiler: Memory control stream started.
 V/StudioProfiler: Live memory tracking disabled.
 V/StudioProfiler: Live memory tracking enabled.
    JNIEnv not attached
 V/StudioProfiler: Loaded classes: 5492
 V/StudioProfiler: Tracking initialization took: 653867820ns
 D/EGL_emulation: eglMakeCurrent: 0xdc485240: ver 2 0 (tinfo 0xdc4832a0)
 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
 I/chatty: uid=10080(x.glamit2) Binder:14399_4 identical 1 line
 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
 W/zygote: Verification of void android.support.v4.view.AbsSavedState.<clinit>() took 590.693ms
 I/zygote: CollectorTransition concurrent copying GC freed 3073(829KB) AllocSpace objects, 0(0B) LOS objects, 51% free, 1435KB/2MB, paused 100.074ms total 209.069ms
 I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
 I/OpenGLRenderer: Initialized EGL, version 1.4
 D/OpenGLRenderer: Swap behavior 1
 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
 D/OpenGLRenderer: Swap behavior 0
 D/EGL_emulation: eglCreateContext: 0xdc485240: maj 2 min 0 rcv 2
 D/EGL_emulation: eglMakeCurrent: 0xdc485240: ver 2 0 (tinfo 0xdc4832a0)
 I/Choreographer: Skipped 63 frames!  The application may be doing too much work on its main thread.
 D/EGL_emulation: eglMakeCurrent: 0xdc485240: ver 2 0 (tinfo 0xdc4832a0)
 I/zygote: Do partial code cache collection, code=27KB, data=30KB
 I/zygote: After code cache collection, code=27KB, data=30KB
    Increasing code cache capacity to 128KB

2 Answers2

1

you want pass image one activity to another activity right?

Yes, I want to pass the bitmap image from the main activity to the second activity!

So when you get image don't need to set image in MainActivity

need to intent for SecondActivityand send bitmap using bundle

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        //imageView.setImageBitmap(photo);/* no need of it*/

        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("BitmapImage", photo);


    }
}

Updated

and receive bitmap to SecondActivity and set it in onCreate()

public class Glamme extends AppCompatActivity {
    private ImageView imageView;
 Bitmap bitmap=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glamme);
        imageView = (ImageView) findViewById(R.id.takepicture);


        Intent intent = getIntent();
        if(intent!=null)
            bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

     if(bitmap!=null)
        imageView.setImageBitmap(bitmap);
}
Adil
  • 812
  • 1
  • 9
  • 29
0

You are not doing in the right way.You pass the bitmap for onActivityResult().not from onCreate();

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        Bitmap image = (Bitmap) data.getExtras().get("data");
        //imageView.setImageBitmap(photo);/* no need of it*/

        Intent intent = new Intent(MainActivity.this, Glamme.class);
        intent.putExtra("BitmapImage", image);


    }
}
kundan kamal
  • 674
  • 7
  • 16