在“警报”对话框中创建类似配置文件图片的图像

时间:2017-07-20 20:37:52

标签: android alertdialog android-image

我正在尝试在警报对话框中创建个人资料图片。您将在下面看到Dialog的图片和我正在使用的代码。有人可以帮我解决这个问题。此警报对话框位于活动内。此活动已获取许可证图片的图像

username_dialog screenshot

username_dialog.xml 即可。仅包含与图像视图和文本相关的代码

   <LinearLayout
    android:layout_width="288dp"
    android:layout_height="80dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <ImageView
        android:id="@+id/profile_pic"
        android:layout_width="120dp"
        android:layout_height="75dp"
        android:background="#CED5E4"
        android:src="@drawable/user_placeholder" />

    <TextView
        android:paddingTop="5dp"
        android:layout_width="145dp"
        android:layout_height="78dp"
        android:layout_marginLeft="10dp"
        android:textSize="16dp"
        android:textColor="#FFFFFF"
        android:textAlignment="center"
        android:text="Press the Take Pic button and create your Profile Pic"
        android:layout_weight="0.23">

    </TextView>

</LinearLayout>

bDriverRegistrationActivity 与问题相关的代码。

 private ImageButton license_photo;
 private Uri filepath;
 private StorageReference storageRef;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b_driver_registration);

    firebaseAuth = FirebaseAuth.getInstance();
    storageRef = FirebaseStorage.getInstance().getReference();

    license_photo = (ImageButton) findViewById(R.id.license_photo);
    license_photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intentCamera, 0);
        }
    });

    register_btn = (Button) findViewById(R.id.register_btn);
    register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            attemptLogin(true);
            uploadFile();
        }
    });


} // onCreate

uploadFile()

public void uploadFile() {

    if (filepath != null) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading ...");
        progressDialog.show();

        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

        StorageReference licenseRef = storageRef.child("drivers").child(userId)
                .child("images/license.jpg");

        licenseRef.putFile(filepath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(), "File Uploaded", Toast
                                .LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(), exception
                                .getMessage(), Toast.LENGTH_LONG).show();
                    }
                })

                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0 * taskSnapshot
                                .getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                        progressDialog.setMessage(((int) progress) + "% Uploaded...");
                    }
                });

    } else {
        // display an error toast
    }

} // uploadFile()

UsernameDialogFragment

public static class UsernameDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.username_dialog, null));

                // Add action buttons ...

                builder.setPositiveButton(R.string.action_register, new DialogInterface.OnClickListener() {
                    @Override
                    public final void onClick(final DialogInterface dialog, int id) {

                        // save the username to Firebase and sign in user  ...

                        // ... casting dialog interface to an alert dialog and casting
                        // the result of the findView to an EditText
                        EditText usernameField  = (EditText)((AlertDialog) dialog)
                                .findViewById(username);
                        String username = usernameField.getText().toString();

                        // year
                        EditText yearField = (EditText)((AlertDialog) dialog).findViewById(R.id.year);
                        String year = yearField.getText().toString();

                        // color, make and model
                        EditText cmmField = (EditText)((AlertDialog) dialog).findViewById(R.id.cmm);
                        String cmm = cmmField.getText().toString();

                        // cell
                        EditText cellField = (EditText)((AlertDialog) dialog).findViewById(R.id.cell);
                        String cell = cellField.getText().toString();

                        // license plate no.
                        EditText plateField = (EditText)((AlertDialog) dialog).findViewById(R.id.licenseNo);
                        String licenseNo = plateField.getText().toString();

                        // profile pic
                        ImageView profilePic = (ImageView)((AlertDialog) dialog).findViewById(R.id.profile_pic);
                        profilePic.setImageResource(R.drawable.user_placeholder);



                        // ... get user's unique id
                        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

                        User aUser = new User(username, year, cmm, cell, licenseNo);


                        /*  https://android-chat-af94c.firebaseio.com/android-chat-af94c/
                            users/pRsxsToJZPTzCdtft69f1grIJC13/profile/username

                            getInstance -> grabbing the url:
                            https://android-chat-af94c.firebaseio.com/android-chat-af94c/
                        */
                        // above is the same as below ...
                        FirebaseDatabase.getInstance().getReference("drivers").child(userId).setValue(aUser);

                        Intent intent = new Intent(getActivity().getBaseContext(), PoliciesActivity.class);
                        startActivity(intent);
                    }

                });

                builder.setNeutralButton(R.string.action_take_pic, new DialogInterface.OnClickListener() {
                    @Override
                    public final void onClick(final DialogInterface dialog, int id) {


                    }
                });

        return builder.create();

    }

} // UsernameDialogFragment

onActivityResult 现在这适用于许可照片

// take photo
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)  {
    super.onActivityResult(requestCode, resultCode, data);

    filepath = data.getData();

    Bitmap bitmap = (Bitmap)data.getExtras().get("data");
    license_photo.setImageBitmap(bitmap);

} // onActivityResult

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我不确定你真正想要存档的内容。但是,在AFAIK中,您应该保存对话框ImageView以随时更改其图像:

View dialogView = inflater.inflate(R.layout.username_dialog, null);
// save imageview as global variable 
ImageView avatar = dialogView.findViewId(R.id.imageview_avatar);
builder.setView(dialogView);
//you can change imageview later after user picks image.
Glide....into(avatar);