首页 文章

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

提问于
浏览
1

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

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

这个小小的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 回答

  • 1

    你没有以正确的方式做 . 你传递了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);
    
    
        }
    }
    
  • 0

    你想把图像一个活动传递到另一个活动吗?

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

    因此,当您获得图像时,不需要在MainActivity中设置图像

    需要意图 SecondActivity 并使用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

    并接收 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);
    }
    

相关问题