从片段中的相机意图返回后,屏幕变为白色

时间:2015-02-12 05:10:56

标签: android android-fragments camera imageview

我正在经历一个奇怪的情况。请帮帮我。

我正在开发一个应用程序,用户必须从相机中捕获两张图像才能注册用户照片和地址照片。

当我尝试从背面相机捕捉图像时。我能成功地做到这一点。但是当我使用移动设备屏幕的前置摄像头拍摄第二张图像后变成白色

PS:注册表格是片段。

我已经搜索过了,但无法找到任何解决方案。帮我解决一下。

enter image description here

源代码:

private void captureImage(int ImageCode) {

    Context context = getActivity();
    PackageManager packageManager = context.getPackageManager();
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) == false
            && packageManager
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY) == false) {
        Toast.makeText(getActivity(), R.string.camera_is_not_available,
                Toast.LENGTH_SHORT).show();
        return;
    }

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent
            .resolveActivity(getActivity().getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Toast.makeText(getActivity(), R.string.error_connecting_camera,
                    Toast.LENGTH_SHORT).show();
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            if (ImageCode == CAPTURE_MEMBER_PHOTO_REQUEST_CODE) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent,
                        CAPTURE_MEMBER_PHOTO_REQUEST_CODE);

                img_reg_member_photo.setImageBitmap(null);
                mMemberPhotoPath = null;
                memberImageflag = false;
            }
            if (ImageCode == CAPTURE_ADDRESS_PHOTO_REQUEST_CODE) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent,
                        CAPTURE_ADDRESS_PHOTO_REQUEST_CODE);

                img_reg_address_proof.setImageBitmap(null);
                mAddressPhotoPath = null;
                addressImageflag = false;
            }
        }
    }
}

@SuppressLint("SimpleDateFormat")
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);

    mPhotoPath = image.getAbsolutePath();

    Log.v("createImageFile", "" + mPhotoPath);
    return image;
}

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

    if (requestCode == CAPTURE_MEMBER_PHOTO_REQUEST_CODE
            && resultCode == Activity.RESULT_OK) {

        File imgFile = new File(mPhotoPath);
        if (imgFile.exists()) {

            mMemberPhotoPath = mPhotoPath;
            BitmapFactory.Options bitmap_options = new BitmapFactory.Options();
            bitmap_options.inSampleSize = 4;
            bitmap_options.outHeight = 200;
            bitmap_options.outWidth = 200;

            Bitmap myBitmap = BitmapFactory.decodeFile(
                    imgFile.getAbsolutePath(), bitmap_options);
            img_reg_member_photo.setImageBitmap(myBitmap);

            if (img_reg_member_photo.getDrawable() == null) {
                Log.e("MEMBER IMAGE VIEW", "NULL MEMBER IMAGE");
                memberImageflag = false;
            } else {
                Log.e("MEMBER IMAGE VIEW", "MEMBER IMAGE VIEW UPDATED");
                memberImageflag = true;
            }
        }
    } else if (requestCode == CAPTURE_MEMBER_PHOTO_REQUEST_CODE
            && resultCode == Activity.RESULT_CANCELED) {

        Toast.makeText(getActivity(), R.string.camera_operation_canceled,
                Toast.LENGTH_SHORT).show();

        memberImageflag = false;
        img_reg_member_photo.setImageBitmap(null);

        Log.e("MEMBER IMAGE VIEW", "CANCEL EVENT - NULL MEMBER IMAGE"
                + img_reg_member_photo.getDrawable());

    }

    if (requestCode == CAPTURE_ADDRESS_PHOTO_REQUEST_CODE
            && resultCode == Activity.RESULT_OK) {

        File imgFile = new File(mPhotoPath);
        if (imgFile.exists()) {

            mAddressPhotoPath = mPhotoPath;
            BitmapFactory.Options bitmap_options = new BitmapFactory.Options();
            bitmap_options.inSampleSize = 4;
            bitmap_options.outHeight = 200;
            bitmap_options.outWidth = 200;

            Bitmap myBitmap = BitmapFactory.decodeFile(
                    imgFile.getAbsolutePath(), bitmap_options);

            img_reg_address_proof.setImageBitmap(myBitmap);

            if (img_reg_address_proof.getDrawable() == null) {
                Log.e("ADDRESS IMAGE VIEW", "NULL ADDRESS IMAGE");
                addressImageflag = false;
            } else {
                Log.e("ADDRESS IMAGE VIEW", "ADDRESS IMAGE VIEW UPDATED");
                addressImageflag = true;
            }
        }
    } else if (requestCode == CAPTURE_ADDRESS_PHOTO_REQUEST_CODE
            && resultCode == Activity.RESULT_CANCELED) {

        Toast.makeText(getActivity(), R.string.camera_operation_canceled,
                Toast.LENGTH_SHORT).show();

        addressImageflag = false;
        img_reg_address_proof.setImageBitmap(null);

        Log.e("ADDRESS IMAGE VIEW", "CANCEL EVENT - NULL ADDRESS IMAGE"
                + img_reg_address_proof.getDrawable());

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<com.equesttech.mybyk.slidingfragments.FractionalLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_label_navigate"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@drawable/box_green"
        android:gravity="center"
        android:paddingBottom="15dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:text="@string/tap_to_close"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:animateLayoutChanges="true"
            android:padding="10dp" >

            <TextView
                android:id="@+id/txt_label_user_registration"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/register_member"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/actionbar"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txt_member_acc_details"
                style="?android:attr/listSeparatorTextViewStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:padding="5dp"
                android:text="@string/member_acc_details"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/edt_reg_firstname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/box_edittext"
                android:ems="10"
                android:hint="@string/first_name"
                android:padding="10dp" >
            </EditText>

            <EditText
                android:id="@+id/edt_reg_lastname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/box_edittext"
                android:ems="10"
                android:hint="@string/last_name"
                android:padding="10dp" >
            </EditText>

            <TextView
                android:id="@+id/txt_gender"
                style="?android:attr/listSeparatorTextViewStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                android:text="@string/gender"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold" />

            <RadioGroup
                android:id="@+id/radio_grp_gender"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/radio_male"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/male" />

                <RadioButton
                    android:id="@+id/radio_female"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/female" />
            </RadioGroup>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:visibility="visible" >

                    <EditText
                        android:id="@+id/edt_reg_dob"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/box_edittext"
                        android:clickable="false"
                        android:ems="10"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:hint="@string/dob"
                        android:inputType="datetime"
                        android:padding="12dp"
                        android:textColor="@color/black" />

                    <Button
                        android:id="@+id/btn_delete_reg_dob"
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        android:layout_alignBaseline="@+id/edt_reg_dob"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:background="@drawable/delete_item"
                        android:gravity="center_vertical"
                        android:visibility="gone" />
                </RelativeLayout>

                <Button
                    android:id="@+id/btn_reg_dob"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:background="@drawable/box_cyan"
                    android:padding="10dp"
                    android:text="@string/date"
                    android:textColor="@color/white" />
            </LinearLayout>

            <EditText
                android:id="@+id/edt_reg_contactnumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/box_edittext"
                android:ems="10"
                android:hint="@string/contact_number"
                android:inputType="number"
                android:maxLength="10"
                android:padding="10dp" >
            </EditText>

            <TextView
                android:id="@+id/txt_member_documents"
                style="?android:attr/listSeparatorTextViewStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                android:text="@string/member_documents"
                android:textStyle="bold" />

            <LinearLayout
                android:id="@+id/view_current_photo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="visible" >

                <TextView
                    android:id="@+id/txt_current_photo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:padding="5dp"
                    android:text="@string/current_photo"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/btn_select_member_photo"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/box_red"
                    android:padding="10dp"
                    android:text="@string/capture_image"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@+id/img_reg_member_photo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:contentDescription="@string/address_proof"
                    android:scaleType="centerCrop" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/view_driving_license"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="visible" >

                <TextView
                    android:id="@+id/txt_driving_license"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:padding="5dp"
                    android:text="@string/address_proof"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/btn_select_driving_license"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/box_red"
                    android:padding="10dp"
                    android:text="@string/capture_image"
                    android:textColor="@color/white" />

                <ImageView
                    android:id="@+id/img_reg_address_proof"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:contentDescription="@string/address_proof"
                    android:scaleType="centerCrop" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="5dp" >

                <Button
                    android:id="@+id/btn_submit"
                    style="?android:attr/buttonBarButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:background="@drawable/box_green"
                    android:padding="10dp"
                    android:text="@string/submit"
                    android:textColor="@color/white" />

                <Button
                    android:id="@+id/btn_clear"
                    style="?android:attr/buttonBarButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:background="@drawable/box_cyan"
                    android:padding="10dp"
                    android:text="@string/clear"
                    android:textColor="@color/white" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</com.equesttech.mybyk.slidingfragments.FractionalLinearLayout>

1 个答案:

答案 0 :(得分:-1)

这是我的应用程序中的一个工作代码,它从前后凸轮捕获图像并在片段内使用。我有额外的图像裁剪功能请在使用时避免使用,如果没有必要

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

                if(taken_image==null) {

                    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);

                    dialog.cancel();
                    mEdit.setVisibility(View.VISIBLE);
                }

            }
        });


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

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK&&requestCode==1) {

            if(image_uri!=null)
                try {
                   capture=true;

                    performCrop(image_uri);

                } catch (OutOfMemoryError e) {
                    Toast.makeText(getActivity(), e + "\"memory exception occured\"", Toast.LENGTH_LONG).show();

                    taken_image = null;
                    round_Image = null;
                    upload_image_hd = null;

                } catch (Exception e) {
                    Toast.makeText(getActivity(), e + "\"exception occured\"", Toast.LENGTH_SHORT).show();
                    taken_image = null;
                    round_Image = null;
                    upload_image_hd = null;

                }


        }
 else if (requestCode == 3) {  //after cropping of profilepic
            // get the returned data
            try {
            Bundle extras = data.getExtras();
            // get the cropped bitmap
          Bitmap  thePic = extras.getParcelable("data");
            }
            catch(Exception e){
                System.out.println(e);
            }


            if(capture){

            ExifInterface exif = null;
            try {
                exif = new ExifInterface(image_uri.getPath());
               //Since API Level 5
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            //TODO
            upload_image_hd =BitmapFactory.decodeFile(image_uri.getPath());
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                Rotate90Bitmap(thePic,upload_image_hd, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                Rotate180Bitmap(thePic,upload_image_hd, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                Rotate270Bitmap(thePic,upload_image_hd, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
                RotateBitmap(thePic,upload_image_hd, 0);
                break;
            default:
                RotateBitmap(thePic,upload_image_hd, 0);
                break;
        }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            catch (OutOfMemoryError e) {
                Toast.makeText(getActivity(), e + "\"memory exception occured\"", Toast.LENGTH_LONG).show();

                taken_image = null;
                round_Image = null;
                upload_image_hd = null;

            } catch (Exception e) {

                taken_image = null;
                round_Image = null;
                upload_image_hd = null;

            }  
            }

    }



/**
     * this function does the crop operation.
     */
    private void performCrop(Uri picUri) {
        // take care of exceptions
        try {
            // call the standard crop action intent (the user device may not
            // support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP");           
            // indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            // set crop properties
            cropIntent.putExtra("crop", "true");
            // indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 2);
            cropIntent.putExtra("aspectY", 1);
            // indicate output X and Y
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, 3);
        }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            Toast toast = Toast
                    .makeText(getActivity(), "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
            toast.show();
        }
        }



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

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),Base64.NO_WRAP);
        Bitmap cropimg=Bitmap.createBitmap(crop, 0, 0, crop.getWidth(), crop.getHeight(), matrix, true);
        imgcroppedString=Base64.encodeToString(getBytesFromBitmap(cropimg),Base64.NO_WRAP);
        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }

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

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
        Bitmap cropimg=Bitmap.createBitmap(crop, 0, 0, crop.getWidth(), crop.getHeight(), matrix, true);
        imgcroppedString=Base64.encodeToString(getBytesFromBitmap(cropimg),Base64.NO_WRAP);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),Base64.NO_WRAP);
        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }

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

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
        Bitmap cropimg=Bitmap.createBitmap(crop, 0, 0, crop.getWidth(), crop.getHeight(), matrix, true);
        imgcroppedString= Base64.encodeToString(getBytesFromBitmap(cropimg),Base64.NO_WRAP);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),Base64.NO_WRAP);

        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }

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

        round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
        // get the base 64 string
        imgString = Base64.encodeToString(getBytesFromBitmap(round_Image),Base64.NO_WRAP);

        Bitmap cropimg=Bitmap.createBitmap(crop, 0, 0, crop.getWidth(), crop.getHeight(), matrix, true);
        imgcroppedString= Base64.encodeToString(getBytesFromBitmap(cropimg),Base64.NO_WRAP);

        uploadProfilePicture_bytes();
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }