Firebase存储,onSuccessListener没有按预期执行

时间:2017-04-04 12:51:27

标签: android firebase firebase-storage event-listener

我一直在尝试创建一个使用Firebase身份验证,Firebase存储和Firebase数据库的应用。我试图从successListener获取downloadURI,但它永远不会到达,并且toast永远不会显示。但是,在Firebase Storage中,它已成功创建了包含profilepictures的文件夹并上传了该图像。我不明白我做错了什么,所以任何帮助都表示赞赏!

public class RegisterLastActivity extends AppCompatActivity {

private EditText displayName;
private Button cheersButton;
private ImageView profilePicture;

private Uri imageFile;
private boolean imageFileChosen = false;

private StorageReference mStorageRef;
private DatabaseReference mDatabase;

private static final int PICK_IMAGE_REQUEST = 1;

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

    displayName = (EditText) findViewById(R.id.displayname);
    cheersButton = (Button) findViewById(R.id.cheers_button);
    profilePicture = (ImageView) findViewById(R.id.profile_picture);

    mStorageRef = FirebaseStorage.getInstance().getReference();
    mDatabase = FirebaseDatabase.getInstance().getReference();

    cheersButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendToStorage(imageFile);
            startActivity(new Intent(RegisterLastActivity.this, MainActivity.class));
            finish();

        }
    });
}

public void imagePressed(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);    }

@Override
protected 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) {
        Uri uri = data.getData();
        setImageFile(uri);
        imageFileChosen = true;
        Log.d("OnActivityResult", "imageFileChosen " + String.valueOf(imageFileChosen));
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            //Log.d(TAG, String.valueOf(bitmap));

            profilePicture.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

protected void sendToStorage(Uri uri){
    if(imageFileChosen){
        StorageReference storageReference = mStorageRef.child("Profilepictures").child(getCurrentToken());
        storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                @SuppressWarnings("VisibleForTests") Uri downloadUri = taskSnapshot.getDownloadUrl();
                Toast.makeText(RegisterLastActivity.this, "Kom inn til success!", Toast.LENGTH_SHORT).show();
            }
        });
    }else{
        Toast.makeText(this, "Choose an image!", Toast.LENGTH_SHORT).show();
    }
}

protected String getCurrentToken(){
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if(user != null){
        return user.getUid();
    }
    return null;
}

public void setImageFile(Uri imageFile) {
    this.imageFile = imageFile;
}
}

这不会在Android Monitor中产生任何错误消息。查找sendToStorage(Uri uri)方法,即我有错误的地方。我不得不包括@SuppressWarnings(&#34; VisibleForTests&#34;),因为它给了我一个恼人的错误。

0 个答案:

没有答案