简体   繁体   English

在android中将图像从一个活动传递到另一个活动

[英]Passing image from one activity to another in android

I want to create a camera application where I want an image to be captured and displayed to another Activity .我想创建一个相机应用程序,我想在其中捕获图像并将其显示给另一个Activity

Here is the code-这是代码-

public class MainActivity extends AppCompatActivity {
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button photoclick= (Button) findViewById(R.id.clickphoto);
        Button recordgetter= (Button) findViewById(R.id.getrecord);
        Typeface font= Typeface.createFromAsset(getAssets(), "insomnia.ttf");
        photoclick.setTypeface(font);
        recordgetter.setTypeface(font);
        photoclick.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dispatchTakePictureIntent();
            }
        });
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, 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();
            Bitmap imageBitmap = (Bitmap) extras.get("data");

            Intent i=new Intent(this,Description.class);
            i.putExtra("BitmapImage", imageBitmap);
            startActivity(i);
        }
    }
}

Here is the second class where I want the image to be displayed-这是我希望显示图像的第二个类-

public class Description extends AppCompatActivity {
    ImageView capturedimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent i=getIntent();
        Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");

        capturedimage=(ImageView) findViewById(R.id.capturedImage);
        capturedimage.setImageBitmap(bitmap);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
    }
}

Here is the XML code of second activity-这是第二个活动的 XML 代码-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_description"
    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.rishabhgambhir.findthelost.Description">

    <ImageView
        android:id="@+id/capturedImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@color/colorPrimaryDark" />
</RelativeLayout>

The application is forced stop as soon as I click the picture and press the tick mark.一旦我单击图片并按下刻度线,应用程序就会被强制停止。 Please help.请帮忙。

00:22:33.615 19175-19175/com.example.rishabhgambhir.findthelost E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.rishabhgambhir.findthelost, PID: 19175
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rishabhgambhir.findthelost/com.example.rishabhgambhir.findthelost.Description}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                                        at com.example.rishabhgambhir.findthelost.Description.onCreate(Description.java:24)
                                                                                        at android.app.Activity.performCreate(Activity.java:6251)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method)

Make you Description class like让你像Description

public class Description extends AppCompatActivity {
    ImageView capturedimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
        Intent i=getIntent();
        Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");
        capturedimage=(ImageView) findViewById(R.id.capturedImage);
        capturedimage.setImageBitmap(bitmap);

    }
}

You can't call findViewById() before setContentView() .您不能在setContentView()之前调用findViewById() setContentView() That's a crash for sure.那肯定是崩溃了。 Post logcat if it's something else如果是别的东西,请发布 logcat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM