onActivityResult resultCode返回null

时间:2019-04-23 16:37:26

标签: android firebase android-intent firebase-realtime-database

对我来说,这是一个令人失望的错误,下面是单击按钮的实现,该按钮将选择要上传到我的Firestore中的图像,但是我的结果代码为-1。

mAddMessageImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Select image for image message on click.
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            startActivityForResult(intent, REQUEST_IMAGE);
        }
    });

最后是罪魁祸首 onActivityResult

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

    Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
    if (requestCode == REQUEST_IMAGE) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                final Uri uri = data.getData();
                Log.d(TAG, "Uri: " + uri.toString());
                FriendlyMessage tempMsg =
                        new FriendlyMessage(null, mUsername, mPhotoUrl, LOADING_IMAGE_URL);
                mFirebaseDatabaseReference.child(MESSAGES_CHILD)
                        .push().setValue(tempMsg, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
                        if (databaseError == null) {
                            String key = databaseReference.getKey();
                            StorageReference storageReference = FirebaseStorage.getInstance()
                                    .getReference(mFirebaseUser.getUid())
                                    .child(key)
                                    .child(uri.getLastPathSegment());
                            putImageInStorage(storageReference, uri, key);
                        } else {
                            Log.w(TAG, "Unable to write message to database.", databaseError.toException());
                        }
                    }
                });

            }
        }
    }
}

每次单击按钮都会不断上传占位符硬编码图像,请保存我!

3 个答案:

答案 0 :(得分:0)

您可以尝试将其包装在try / catch块中:

{{1}}

答案 1 :(得分:0)

尝试这种方式

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) 
{
super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE && resultCode  == RESULT_OK) 

答案 2 :(得分:0)

您希望Predicate为-1,因为那是Activity.RESULT_OK