Pergunta

I am trying to make an android application in which i make a information filling form in which user fill the in formation.and also i gave a camera button for capture photo and save it.

I want when user click camera button camera open and take picture and save it.

I do not open another activity on camera button click.and also i do not want another xml layout file for camera.means i want when i press camera button camera open in same layout and save pic.

How can i do this.

Here is my Code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bl"
android:orientation="vertical"
android:weightSum="1.0" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content "
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight=".05"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginTop="8dp"
            android:layout_weight=".02"
            android:background="@drawable/keypad_select"
            android:orientation="horizontal"
            android:weightSum="1.0" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight=".50"
                android:text="FAULT TYPE:-"
                android:textColor="@color/orange"
                android:textSize="25sp" />

            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="0dp"
                android:layout_height="49dp"
                android:layout_weight="0.50" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginTop="8dp"
            android:layout_weight=".05"
            android:background="@drawable/keypad_select"
            android:orientation="horizontal"
            android:weightSum="1.0" >

            <TextView
                android:id="@+id/Button01"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".50"
                android:text="" />

            <Button
                android:id="@+id/save_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.19"
                android:background="@drawable/btn_white_glossy"
                android:text="SAVE.." />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

Foi útil?

Solução

Use this for starting the camera app of the phones of tables

// called when the user selects to take a new picture 
public void take_Pic(final View view) {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);



}

Then later on in the activity:

/*
 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
 * the result for snapping a new picture or selecting one from the gallery 
 */

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

                  if (resultCode == RESULT_OK) {

                    myBitmap = data.getExtras().getParcelable("data");
                     ImageView photo = (ImageView) findViewById(R.id.pic);
                     photo.setImageBitmap(myBitmap);// here I am setting the pic to an image view for the user to have a look.  

                }

            }
     }

You dont have to make a new class or layout for the `Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); bit. That just opens the camera app of the phone or tablet.

Hope this helps `

Outras dicas

Start your camera intent inside of some imageview click:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
 startActivityForResult(intent, CAPTURE_NEW_PICTURE);

Then in onActivityResult:

 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 mImage = (ImageView) findViewById(R.id.imageView1);
 mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));

Here is your decodeSampledBitmapFromFile

public static Bitmap decodeSampledBitmapFromFile(String path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
  }

Above lines are explained like below:

1. When you click on image view, it start your camera intent. 
2. you can take photo using camera intent and save it.
3. Then it save into your SD card.
4. Finally you can retrieve your saved image from SD card and apply to your imageview.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top