2

Possible Duplicate:
Calling camera from an activity, capturing an image and uploading to a server

i am very new to android, i am trying to capture the image after that i placed that image into image view,its working on emulator but in device it crashes it shows the message force close even it cannot open the camera activity.the following is my code. please help me how to solve this problem.thank you.

package com.gss.android;

package com.gss.android;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Pictures extends Activity{

    Button btnsave,btnnewpic;
    protected ImageView image;
    protected String _path;
     File sdImageMainDirectory;
    private static final int IMAGE_CAPTURE = 0;
    protected boolean _taken = true;
    private Uri imageUri;
    protected static final String PHOTO_TAKEN = "photo_taken";
    private static final int CAMERA_PIC_REQUEST = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pictures);
        btnsave=(Button)findViewById(R.id.btnsave);
        btnnewpic=(Button)findViewById(R.id.btnnewpic);




        btnnewpic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                startCameraActivity2();
            }
        });

    }

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

            if ( requestCode == CAMERA_PIC_REQUEST){
                if (resultCode == RESULT_OK){
                    switch (resultCode) {
                    case 0:
                           finish();
                           break;

                    case -1:

                           try {

                              StoreImage(this, Uri.parse(data.toURI()),
                                           sdImageMainDirectory);
                              Log.e("file path", "stiring file") ;
                               } catch (Exception e) {
                                                 e.printStackTrace();
                                }

                                    finish();


                                 }


            }

            } else if ( requestCode == IMAGE_CAPTURE) {
                if (resultCode == RESULT_OK){
                    Log.d("ANDRO_CAMERA","Picture taken!!!");
                    ImageView image = (ImageView) findViewById(R.id.image); 
                    image.setImageURI(imageUri);
                }
            } 
     }

        protected void startCameraActivity2()
        {
            Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
            String fileName = "testphoto.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            values.put(MediaStore.Images.Media.DESCRIPTION,
                    "Image capture by camera");
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            startActivityForResult(intent, IMAGE_CAPTURE);

        }





           @Override
           protected void onRestoreInstanceState(Bundle savedInstanceState) {
           if (savedInstanceState.getBoolean(Pictures.PHOTO_TAKEN)) {
           _taken = true;
           }
       }

          @Override
          protected void onSaveInstanceState(Bundle outState) {
          outState.putBoolean(Pictures.PHOTO_TAKEN, _taken);
       }

           public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
           Bitmap bm = null;
               try {

                   bm = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), imageLoc);
                   FileOutputStream out = new FileOutputStream(imageDir);
                   bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    Log.e("file dir",imageDir.toString());

                     bm.recycle();


                    } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    } catch (IOException e) {
                    e.printStackTrace();
                   } catch (Exception e) {
                  e.printStackTrace();
             }

         }

}
Community
  • 1
  • 1
sujana
  • 21
  • 1
  • 3

1 Answers1

3

I think this is a problem related to the emulator and not the physical device.

I used this code and I got a bitmap:

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

        captureButton = (Button)findViewById(R.id.capture);
        captureButton.setOnClickListener(listener);

        imageView = (ImageView)findViewById(R.id.image);

        destination = new   File(Environment.getExternalStorageDirectory(),"image.jpg");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
            //Bitmap userImage = (Bitmap)data.getExtras().get("data");
            try {
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 10; //Downsample 10x
                Bitmap userImage = BitmapFactory.decodeStream(in, null, options);
                imageView.setImageBitmap(userImage);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //Add extra to save full-image somewhere
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
            Toast.makeText(JMABarcodeScannerActivity.this, Uri.fromFile(destination).toString(), Toast.LENGTH_SHORT).show();
            startActivityForResult(intent, REQUEST_IMAGE);
        }
    };
}

Main.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">
      <Button
    android:id="@+id/capture"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Take a Picture"
      />
      <ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="centerInside"
       />
          </LinearLayout>