0

Hi I'm using a camera application in Android. I want to pass the byte data from PictureCallback method to another activity and want to display it in that activity. I used the following code:

Camera.PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Intent i = new Intent(FbCamera.this, view.class);
               i.putExtra("photo", data);
               Log.d(TAG, "jpegCallback" +data);
               startActivity(i);
        }
    };

and second class view.class as shown bellow,

public class view extends Activity {    
    private static final String TAG = "Camera";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        Bundle extras = getIntent().getExtras();
        byte[] photo = extras.getByteArray("photo");
        Log.i(TAG, "jpegCallback2" + photo);
        Bitmap bitmap  = BitmapFactory.decodeByteArray (photo, 0, photo.length);
        ImageView imgView = (ImageView)findViewById(R.id.photoResultView);
        imgView.setImageBitmap(bitmap);
    }
}

This is my layout main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageView android:layout_height="fill_parent" 
           android:id="@+id/photoResultView" 
           android:src="@drawable/icon" 
           android:layout_width="fill_parent"></ImageView>
</LinearLayout>

when I am running this code, force_close occurs after the camera click. If anyone knows about it please help me.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
Binesh
  • 117
  • 3
  • 15

1 Answers1

0

This code should be inserted where you start your camera activity .. like say on some button click.. it ll open your camera-

public void takePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    //You save your image in your sdcard by name "Pic.jpg"
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);  //Save uri that is path of your image
    startActivityForResult(intent, TAKE_PICTURE);
}

You can then override onActivityResult method like this -

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri; //Get image path                          
            getContentResolver().notifyChange(selectedImage, null);
            btn = (ImageButton) findViewById(R.id.camera);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                btn.setImageBitmap(bitmap); // I have displayed that image on imagebutton
                 // you can display anyware you want like imageview
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }

}
Rohit
  • 2,538
  • 6
  • 28
  • 41
  • Hi rohit, In my application, i dont want to save my image to sd card. I just want to pass the image that captured to another activity and disply it there. I did the code as shown above. In that code i can acess the byte data of the image to second activity. I want to convert it to bitmap image and want to display it in an imageview. But errors occurs. Do you know about it? – Binesh Nov 09 '11 at 08:54
  • Most probably if decodeByteArray() cant decode then it can give error.. I ll let you know if I find something.. :) – Rohit Nov 09 '11 at 09:05
  • are you using emulator or device ? You should try it on device if u can – Rohit Nov 09 '11 at 09:10
  • if something worked for you.. then can you share it ? otherwise you can accept the answer ^^ – Rohit Nov 09 '11 at 09:30