尝试为当前用户Firebase Auth选择配置文件图片时不断出现错误

时间:2018-08-02 14:00:34

标签: android firebase firebase-authentication

我一直收到相同的错误,当我尝试选择要在Android上使用google firease firebaseAuth为用户上传的图片时,应用停止运行。这是我不断收到的错误:原因:java.lang.ClassNotFoundException:找不到类“ com.google.android.gms.common.internal.zzbq”

我的代码如下:

public class StudentAccount extends AppCompatActivity {


private static final int CHOOSE_IMAGE = 101;
public static final int CAMERA_REQUEST_CODE = 10;
public static final int PROFILE_PIC_REQUEST_CODE = 20;

private Button button, signout;

private FirebaseAuth mAuth;
TextView username;

ImageView imageView;
EditText editText;
Uri uriProfileImage;
ProgressBar progressBar;
String profileImageUrl;


ListView listView;

private List<String> userList = new ArrayList<>();


private final int BARCODE_RECO_REQ_CODE = 200;

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

    mAuth = FirebaseAuth.getInstance();
    signout = (Button) findViewById(R.id.SIGNOUT3);
    username = (TextView) findViewById(R.id.welcomeText2);

    //Check if user i already logged in or not

    if (mAuth.getCurrentUser() == null){
        finish();
        startActivity(new Intent(getApplicationContext(),SignInActivity.class));
    }

    //Fetching display name of current user and setting to activity
    FirebaseUser user = mAuth.getCurrentUser();
    if (user != null){
        username.setText("Welcome " +user.getEmail());
    }

    signout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAuth.signOut();
            finish();
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
        }
    });


    imageView = findViewById(R.id.imageView);
    progressBar = findViewById(R.id.progressbar);
    mAuth = FirebaseAuth.getInstance();

    button =  findViewById(R.id.VIEW_ATTENDANCE);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openViewMyAttendance();
        }
    });



    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showImageChooser();

        }
    });

    findViewById(R.id.buttonSave).setOnClickListener( new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            saveUserInformation();
        }

        private void saveUserInformation() {
            String displayName = editText.getText().toString();
            if (displayName.isEmpty()){
                editText.setError("Username required");
                editText.requestFocus();
                return;
            }

            FirebaseUser user = mAuth.getCurrentUser();

            if (user != null && profileImageUrl != null){
                UserProfileChangeRequest profile = new UserProfileChangeRequest.Builder()
                        .setDisplayName(displayName).setPhotoUri(Uri.parse(profileImageUrl)).build();

                user.updateProfile(profile).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                      if (task.isSuccessful()) {
                          Toast.makeText(StudentAccount.this, "Profile Updated", Toast.LENGTH_SHORT).show();
                      }
                    }
                });
            }
        }
    });

}

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

    if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() !=null){
        uriProfileImage = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriProfileImage);
            imageView.setImageBitmap(bitmap);

            uploadImageToFirebaseStorage();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (requestCode == BARCODE_RECO_REQ_CODE){
        if (resultCode == RESULT_OK){
            Bitmap photo = (Bitmap)data.getExtras().get("data");
            barcodeRecognition(photo);
        }
    }
}

private void barcodeRecognition(Bitmap photo) {
    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(photo);
    FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance()
            .getVisionBarcodeDetector();
    Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image)
            .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
                @Override
                public void onSuccess(List<FirebaseVisionBarcode> barcodes) {

                    for (FirebaseVisionBarcode barcode: barcodes) {
                        Rect bounds = barcode.getBoundingBox();
                        Point[] corners = barcode.getCornerPoints();

                        String rawValue = barcode.getRawValue();

                        int valueType = barcode.getValueType();
                        Toast.makeText(StudentAccount.this, rawValue, Toast.LENGTH_SHORT).show();

                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                   Toast.makeText(StudentAccount.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                }
            });
}


private void uploadImageToFirebaseStorage() {
    final StorageReference profileImageRef = FirebaseStorage.getInstance().getReference
            ("profilepics/"+System.currentTimeMillis() + ".jpg");

    if (uriProfileImage != null){
        progressBar.setVisibility(View.VISIBLE);
        profileImageRef.putFile(uriProfileImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);

                profileImageUrl = taskSnapshot.getDownloadUrl().toString();

            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(StudentAccount.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }

}

private void loadUserInformation() {
    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        updateProfilePermissions();
    } else {
        String[] permissionRequested = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        requestPermissions(permissionRequested, PROFILE_PIC_REQUEST_CODE);
    }
}

private void updateProfilePermissions() {
    FirebaseUser user = mAuth.getCurrentUser();
    if (user.getPhotoUrl() != null) {
        Glide.with(this).load(user.getPhotoUrl().toString()).into(imageView);
    }
    if (user.getDisplayName() != null) {
        editText.setText(user.getDisplayName());
    }
}


private void showImageChooser(){
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Profile Image"),CHOOSE_IMAGE);
}


@Override
protected void onPause(){
    super.onPause();
}


public void barcodeReco(View v) {
    if(checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
        callsCamera();
    } else {
        String[] permissionRequested = {Manifest.permission.CAMERA};
        requestPermissions(permissionRequested, CAMERA_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == CAMERA_REQUEST_CODE){
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
            callsCamera();
        } else {
            Toast.makeText(this, getString(R.string.unable_to_invoke_camera), Toast.LENGTH_LONG).show();
        }
    } else if (requestCode == PROFILE_PIC_REQUEST_CODE){
        if (grantResults [0] == PackageManager.PERMISSION_GRANTED){
            loadUserInformation();
        } else {
            Toast.makeText( this, getString(R.string.Unable_to_update_profile), Toast.LENGTH_LONG).show();
        }
    }
}

private void callsCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent,BARCODE_RECO_REQ_CODE);
}

public void openViewMyAttendance () {
    Intent intent = new Intent(this, ViewMyAttendance.class);
    startActivity(intent);
}

}

0 个答案:

没有答案