主要活动->相机应用--第二活动显示图片?

时间:2018-07-03 04:09:05

标签: android android-activity bitmap camera display

在我的主要活动中,单击一个按钮可打开“相机”应用程序。在“相机”应用中拍摄照片后,您将进入第二个活动。因此,该按钮将打开相机应用程序和第二个活动。

我希望在第二个活动中显示在第一个活动中拍摄的照片。但是,现在该代码已在主要活动中显示它。

这个小小的android noob非常感谢您的帮助!

我使用了以下摄像机功能的代码:Capture Image from Camera and Display in 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);
                    }
                }

            }

主要活动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>

第二个活动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);
}

第二个活动(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>

日志:

 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(xcevio.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 个答案:

答案 0 :(得分:1)

您要将图像一个活动传递给另一活动吗?

是的,我想将位图图像从主要活动传递到第二个活动!

因此,当您获取图像时,无需在MainActivity中设置图像

需要使用SecondActivity并使用捆绑包发送位图

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


    }
}

已更新

并将bitmap接收到SecondActivity并将其设置在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);
}

答案 1 :(得分:0)

您的操作方式不正确。您传递了onActivityResult()的位图。不是通过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);


    }
}