当我添加progessDialog时,将图像上传到firebase存储不适用于某些图像

时间:2017-06-17 01:53:14

标签: android firebase firebase-storage

我正在尝试使用下面的代码在我的代码中构建替换配置文件图片功能。它有时工作正常。其他时候,我从我的图库中选择图片后没有任何反应(某些图像)。它似乎是从画廊活动在我选择要上传的图像后崩溃,应用程序只是在logcat中终止而没有任何错误。在我添加进度对话框之前,它曾经工作正常

private void postImage() {

        String path = "userProfiles/" + UUID.randomUUID() + ".jpeg";
        StorageReference userProfilesRef = storage.getReference(path);
        userProfilesRef.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                downloadUrls = taskSnapshot.getDownloadUrl().toString();
                Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
               mProgressDialog.dismiss();
                final String id = firebaseAuth.getCurrentUser().getUid();

                databaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.child("Male").hasChild(id)){

                             databaseReference.child("Male").child(id).child("downloadUrl").setValue(downloadUrls);
                            databaseReference1.child("Male").child(key).child("downloadUrl").setValue(downloadUrls);

                        }
                        else {

                            databaseReference.child("Female").child(id).child("downloadUrl").setValue(downloadUrls);
                            databaseReference1.child("Female").child(key).child("downloadUrl").setValue(downloadUrls);

                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                      //  Toast.makeText(getApplicationContext(), " Uploaded ", Toast.LENGTH_LONG).show();
                    }
                });


            }
        });

  /*      userProfilesRef.putFile(filePath).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), " Upload Failed, Please try again ", Toast.LENGTH_LONG).show();
                //progressDialog.dismiss();

            }
        });*/
    }



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

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();

        CropImage.activity(filePath)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1, 1)
                .start(this);



    }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                mProgressDialog.setMessage("Uploading Please Wait...");
                mProgressDialog.show();
                resultUri = result.getUri();
                postImage();
                image.setImageURI(resultUri);

            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Exception error = result.getError();
            }
        }


}

1 个答案:

答案 0 :(得分:0)

尝试添加

if(mProgressDialog != null)
    mProgressDialog.dismiss();

如果没有错误,那么你错过了对话框声明中的内容