2

I'm new to android, Now I still building an app that consist with Two Activities First is LoginPage and it works well, the second is UserPage the problem starts here. I create Two ImageView on UserPage, what I want is

  1. Click on the ImageView
  2. Take a picture from a Camera
  3. save it to selected ImageView

When I Click the First ImageView and save the picture from camera it works well, The Problem starts when I click the second imageview, when I take a picture and save it to second imageview, the picture in first imageview is gone

This is my code : user.xml

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


<ImageView
    android:id="@+id/image1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="centerInside" />


<ImageView
    android:id="@+id/image2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_toRightOf="@id/image1"
    android:layout_alignTop="@id/image1"
    android:scaleType="centerInside" />

</RelativeLayout>

UserPage.java

public class UserPage extends Activity {


ImageView imageView1, imageView2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user);

    imageView1 = (ImageView)findViewById(R.id.image1);
    imageView1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) { 
            // TODO Auto-generated method stub
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 1);               
        }
    });
    imageView2 = (ImageView)findViewById(R.id.image2);
    imageView2.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) { 
            // TODO Auto-generated method stub
            Intent ii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(ii, 2);                  
        }
    });

}//OnCreate

@Override
protected void onActivityResult(int requestCode,
    int resultCode, Intent data) {
    Bitmap userImage = (Bitmap)data.getExtras().get("data");
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case 1:
       if(resultCode == RESULT_OK){

          Bundle extras = data.getExtras();
          userImage = (Bitmap) extras.get("data");
          imageView1.setImageBitmap(userImage);
       }//if resultCode Case 1
       break;
    case 2:
       if(resultCode == RESULT_OK) {

            Bundle extras = data.getExtras();
            userImage = (Bitmap) extras.get("data");
            imageView2.setImageBitmap(userImage);

        }//if resultCode Case 2

      }//Switch 

   }//onActivityResult  
}//UserPage

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pramana.report"
android:versionCode="2"
android:versionName="1.1" >

<uses-sdk
    android:minSdkVersion="8"        
    />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.pramana.report.UserPage"></activity>
</application>

</manifest>

Please Help, I've already search and try some of suggestion like Take multiple images with camera and insert into multiple diffefent imageViews but it did not work

Sorry for my bad English I'm not too good in English

Community
  • 1
  • 1
Pramana
  • 21
  • 7
  • It did not work either, ImageView1 still lost the image when I save Image to ImageView2 maybe you can give me example, just in case I miss your point – Pramana Aug 21 '14 at 05:50

3 Answers3

0

try this its working:

global declaration

ImageView iv1 ,iv2;
private static Bitmap userImage1,userImage2;

click listener for both Imageview

iv1.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 1);    
        }
    });

    iv2.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 2);    
        }
    });

onActivityResult code

    @Override
protected void onActivityResult(int requestCode,
    int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case 1:
       if(resultCode == RESULT_OK){
           userImage1 = (Bitmap)data.getExtras().get("data");
          Bundle extras = data.getExtras();
          userImage1 = (Bitmap) extras.get("data");
          iv1.setImageBitmap(userImage1);
       }//if resultCode Case 1
       break;
    case 2:
       if(resultCode == RESULT_OK) {
           userImage2 = (Bitmap)data.getExtras().get("data");
            Bundle extras = data.getExtras();
            userImage2 = (Bitmap) extras.get("data");
            iv2.setImageBitmap(userImage2);

        }//if resultCode Case 2

      }//Switch 

   }//onActivityResult 
Yograj Shinde
  • 839
  • 12
  • 23
0

TRY THIS :

private static Bitmap sImageView1 =null;

private static Bitmap sImageView2 =null;

private static Uri image_uri;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

      SavedInstanceState mSavedInstanceState = savedInstanceState;

    if (mSavedInstanceState==null){
        sImageView1=null;
        sImageView2=null;
    }


            imageView1.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) { 

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
                    image_uri = fileUri;
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                    startActivityForResult(intent, 1);       
               }
            });

        }

@Override
public void onStart() {
    super.onStart();

    if (sImageView1 !=null) {
        imageView1.setImageBitmap(userImage);
    } 
    if (sImageView2 !=null) {
        imageView2.setImageBitmap(userImage);
    } 


}

        @Override
        protected void onActivityResult(int requestCode,int resultCode, Intent data) {

        //UPDATE THIS CODE
            switch(requestCode){
             case 1:
                if(resultCode == RESULT_OK){


                    sImageView1=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);

//SAVE YOUR IMAGE IN A SHARED PREFERENCES OR IN ANY LOCAL DATABASE
                    RotateBitmap(sImageView1, 0);

                    //imageView1.setImageBitmap(sImageView1);  

                }//if resultCode Case 1
            break;


            }//Switch 
        }



public Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    sImageView1 = source;
    sImageView1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    imageView1.setImageBitmap(sImageView1);
    imageView1.setAdjustViewBounds(true);


    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
sherin
  • 1,091
  • 2
  • 17
  • 27
  • I tried to edit like yours but the app is stopped when i tried to click ImageView1, any other suggestion sherin? – Pramana Aug 21 '14 at 07:39
  • @Pramana Then what was that error.First you update your imageview1 – sherin Aug 21 '14 at 07:41
  • Fatal Exception: main, java.lang.NullPointerException: file – Pramana Aug 21 '14 at 08:55
  • What I realized is it's even I'm not taking the image the imageView1 is gone, I mean when i click ImageView2 and the when the camera show up I press back button and image in ImageView1 is gone, is the trouble comes when create new Intent? – Pramana Aug 21 '14 at 09:28
  • @Pramana Ok. Then i think you are not saving your first captured image.You need to save image that which you take first because when you try to took the next image without saving the previous image your activity will restart so it will work like a new one only. – sherin Aug 21 '14 at 10:13
  • tq for your response, is there any reference for me to see and learn? – Pramana Aug 21 '14 at 14:00
  • Tq Sherin it's already solved, saving image in shared preferences solved the problem. Even it took a little while but it worked very well thanks again – Pramana Aug 21 '14 at 15:27
  • @Pramana That sounds good. Post your answer or up vote the right one. – sherin Aug 22 '14 at 04:01
0

Thanks to Sherin and Yograj Shinde, the solution is I have to save the activity before start a new one. Here is my code :

UserPage.Java

public class UserPage extends Activity {

ImageView iv1 ,iv2;
private static Bitmap userImage1,userImage2;

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

    if(savedInstanceState != null)
    {
        userImage1 = savedInstanceState.getParcelable("userImage1");
    }

    iv1 = (ImageView)findViewById(R.id.image1);
    iv2 = (ImageView)findViewById(R.id.image2);

    iv1.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 1);    
        }
    });

    iv2.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, 2);    
        }
    });
}

@Override
public void onStart() {
    super.onStart();
    if(userImage1 != null)
    {
        iv1.setImageBitmap(userImage1);
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
   super.onSaveInstanceState(savedInstanceState);
   savedInstanceState.putParcelable("userImage1", userImage1);
}


@Override
protected void onActivityResult(int requestCode,
    int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case 1:
       if(resultCode == RESULT_OK){
          userImage1 = (Bitmap)data.getExtras().get("data");
          Bundle extras = data.getExtras();
          userImage1 = (Bitmap) extras.get("data");
          iv1.setImageBitmap(userImage1);
       }//if resultCode Case 1
       break;
    case 2:
       if(resultCode == RESULT_OK) {
           userImage2 = (Bitmap)data.getExtras().get("data");
            Bundle extras = data.getExtras();
            userImage2 = (Bitmap) extras.get("data");
            iv2.setImageBitmap(userImage2);

        }//if resultCode Case 2

      }//Switch 

   }//onActivityResult 


}//Class UserPage

The idea here is when the start a new activity you must save your last activity to prevent from loss. That's why I using

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
   super.onSaveInstanceState(savedInstanceState);
   savedInstanceState.putParcelable("userImage1", userImage1);
}

to save the data before start new activity, and use onStart() to set ImageView when the new activity start This article will help you to understand :

http://developer.android.com/reference/android/app/Activity.html
How to use onSavedInstanceState example please

Note: The program still need a lot of improvement because it's not finished yet but I hope it will help others

Community
  • 1
  • 1
Pramana
  • 21
  • 7