Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image from capture and show image in another layout using another activity in android

I want to show image after capture by click button Capture in the FirstActivity and show image in the activity_second(layout) using SecondActivity.

FirstActivity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    Button take_photo = (Button) findViewById(R.id.btn_capture);
    take_photo.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(i);
            }
        });
}

Layout activity_first

> <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" >

    <Button
        android:id="@+id/btn_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:text="Capture" />

</RelativeLayout>

SecondActivity

> public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        ImageView view = (ImageView) findViewById(R.id.view_photo);
    }

activity_second

> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/view_photo"
        android:layout_width="260dp"
        android:layout_height="374dp" />

</LinearLayout>
like image 816
kongkea Avatar asked Feb 19 '23 12:02

kongkea


2 Answers

use startActivityForResult() instead of startActivity()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap thumbnail = null;
    if (requestCode == CAMERA_PIC_REQUEST) {
        if (resultCode == RESULT_OK) {
            thumbnail = (Bitmap) data.getExtras().get("data");
            Intent i = new Intent(this, NextActivity.class);
            i.putExtra("name", thumbnail);
            startActivity(i);
        }
    }
}

Next in the next activity try to use this

protected void onCreate(Bundle savedInstanceState) {
    //TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //intialize the image view 

    Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
    //set the image here.
}

Hope this may help you

like image 99
G_S Avatar answered Feb 22 '23 02:02

G_S


You can use the following code to solve the issues :

Your First Activity:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Button take_photo = (Button) findViewById(R.id.btn_capture);
        take_photo.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                // create Intent to take a picture and return control to the calling application
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // start the image capture Intent
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
        {
            Bitmap imageData = null;
            if (resultCode == RESULT_OK) 
            {
                imageData = (Bitmap) data.getExtras().get("data");

                Intent i = new Intent(this, SecondActivity.class);
                i.putExtra("name", imageData );
                startActivity(i);

            } 
            else if (resultCode == RESULT_CANCELED) 
            {
                // User cancelled the image capture
            } 
            else 
            {
                // Image capture failed, advise user
            }
        }
    }

and second Activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Bitmap bitmap  = getIntent().getExtras().getParcelable("name");
        ImageView view = (ImageView) findViewById(R.id.view_photo);
        view.setImageBitmap(bitmap); 
    }

and in Manifest.xml file use the permission:

<uses-feature android:name="android.hardware.camera" />

I hope this will work fine

like image 41
Dragan Petrovic FSD Avatar answered Feb 22 '23 02:02

Dragan Petrovic FSD