0

I'm writing an Android application for taking photos with camera and generating ImageViews for previewing them.

This is how I take pictures and put them in ImageView:

public class TakePhoto extends Activity {

    private static final int CAMERA_REQUEST = 1888; 

    Button btnCaputre;
    ImageView image;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);

         btnCaputre = (Button)findViewById(R.id.btnCapture);
         image = (ImageView)findViewById(R.id.imageView1);

             btnCaputre.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                 startActivityForResult(cameraIntent, CAMERA_REQUEST);      
                 }
            });
            }

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                image.setImageBitmap(photo);          
            }  
        } 

This is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="100dp" >

    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

How to dynamically add more ImageView (for previewing the new photos made) in the same Activity?

EDIT: This is the new edited code with implementation of setOnClickListener for dynamically created ImageView:

public class TakePhoto extends Activity {

    private static final int CAMERA_REQUEST = 1888; 

    Button btnCaputre;
    ImageView image;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);

         btnCaputre = (Button)findViewById(R.id.btnCapture);
         image = (ImageView)findViewById(R.id.imageView1);

             btnCaputre.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                 startActivityForResult(cameraIntent, CAMERA_REQUEST);      
                 }
            });

        image.setOnClickListener(new OnClickListener() {                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub          

                    Log.e("Image id","-->"+ image.getId());

                }
            });
            }

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
          if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
          Bitmap photo = (Bitmap) data.getExtras().get("data"); 
          image = new ImageView(this);
          LayoutParams param=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
          image.setLayoutParams(param);
          image.setImageBitmap(photo);  
          image.setTag(java.util.UUID.randomUUID().toString());
          mLayout.addView(image); }  

        } 
Josef
  • 2,648
  • 5
  • 37
  • 73

1 Answers1

3

For adding Imageview dynamically you need to do following changes in your code

In Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="100dp" >

    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera" />

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>

</LinearLayout>

In Java Code:

In OnCreate(): get the Linearlayout view Viewgroup from the xml

LinearLayout mLayout=(LinearLayout)findViewById(R.id.layout);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                 ImageView image=new ImageView(this);
                 LayoutParam param=new LayoutParam(LayoutParam.WRAP_CONTENT,LayoutParam.WRAP_CONTENT);
image.setLayoutParam(param);
image.setImageBitmap(photo);  
mLayout.addView(image);      
            }  
        }
Shudy
  • 7,806
  • 19
  • 63
  • 98
Droid_Mechanic
  • 1,474
  • 14
  • 13
  • This works great. Thanks. Just one more question is it possible to add click listener on dynamically created ImageView so when I click on image to ged some kind index of object? – Josef Apr 29 '15 at 09:40
  • Yes you can, just set the Tag with each ImageView and on click operation get the tag of view and perform the action accordingly – Droid_Mechanic Apr 29 '15 at 10:00
  • I've already done that but when I put onCick event for ImageView application throws exception: java.lang.NullPointerException. – Josef Apr 29 '15 at 10:06
  • See editet post. When setOnClickListener is implemented then application crashes. – Josef Apr 29 '15 at 10:23
  • 1
    Check that you are using click listener on imageview in onCreate() where as image is initialize in onActivityReseult – Droid_Mechanic Apr 29 '15 at 10:27