0
我正在運行一個奇怪的情況。請幫助我。從片段中的攝像機意圖返回後,屏幕變爲白色
我正在開發一個應用程序,用戶必須從攝像頭捕捉兩個圖像來註冊,即用戶照片和地址照片。
當我嘗試從背面攝像頭捕獲圖像。我可以成功地做到這一點。但是當我使用的前置攝像頭拍攝第二張圖像後,移動設備屏幕變爲白色。
注:註冊表格在片段中。
我已經搜索過它,但找不到任何解決方案。幫助我解決它。
源代碼:
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>
POst你的Xml ....並嘗試設置兩個圖像圖像視圖分開第二個照片捕獲..... – koutuk 2015-02-12 05:19:18