2017-07-20 2 views
0

J'essaie de créer une image de profil dans une boîte de dialogue d'alerte. Ci-dessous vous verrez une photo du dialogue et du code que j'utilise. Quelqu'un peut m'aider à trouver la solution. Cette boîte de dialogue d'alerte est dans une activité. Cette activité est déjà une image pour devient une image licenceCréation d'une image comme une photo de profil dans une boîte de dialogue d'alerte

username_dialog screenshot

de username_dialog.xml. contenant uniquement le code lié à la vue de l'image et le texte

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

Code bDriverRegistrationActivity à la question pertinente.

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 en ce moment cela fonctionne avec photo de licence

// 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 

Toute aide serait grandement appréciée.

Répondre

1

Je ne suis pas sûr de ce que vous voulez vraiment archiver. Cependant, autant que je sache, vous devez enregistrer de votre boîte de dialogue ImageView pour changer son image chaque fois que vous voulez:

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

Je suis en train de permettre à l'utilisateur de sélectionner le bouton pic de prendre et prendre une photo et il remplacera l'image dans l'image vue. En outre, j'utilise un FragmentDialog. – LizG

+0

oui, faites comme je l'ai suggéré ci-dessus. Vous devez stocker la référence à votre 'ImageView' dans le alertDialog. –

+0

Donc, il ne va pas dans le onClick comme tous les autres contrôles, comme les EditTexts et TextViews? – LizG