quickbloxsdk:传递的对象不是文件,内容类型不正确?

quickbloxsdk : Passed object is not file,Incorrect content type?

我正在 android 中开发 quickblox 消息附件功能。

我想做的是:OnViewClicked 一个 CreateChooser(type:images/*) 会打开。 选择图像后,它将进入存储 UrionActivityResult 方法。 Post 正在调用 sendAttachment 函数。

这是我的编码部分:

private Uri uri;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                // Get the Uri of the selected file
                uri = data.getData();
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

public void sendAttachment() {

        File filePhoto = new File(uri.getPath());
        Boolean fileIsPublic = false;

        QBContent.uploadFileTask(filePhoto, fileIsPublic, null, new QBProgressCallback() {
            @Override
            public void onProgressUpdate(int i) {
                // i - progress in percentages
                Log.e(TAG, "onProgressUpdate: " + i);
            }
        }).performAsync(new QBEntityCallback<QBFile>() {
            @Override
            public void onSuccess(QBFile qbFile, Bundle bundle) {

                Integer id = qbFile.getId();

                // create a message
                QBChatMessage chatMessage = new QBChatMessage();
                chatMessage.setProperty("save_to_history", "1"); // Save a message to history

                // attach a photo
                QBAttachment attachment = new QBAttachment("photo");
                attachment.setId(qbFile.getId().toString());
                chatMessage.setBody("");
                chatMessage.addAttachment(attachment);

                // send a message
                try {
                    mQBChatDialog.sendMessage(chatMessage);
                    mtvEmpty.setVisibility(View.INVISIBLE);
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(QBResponseException errors) {
                Log.e("DATA", TextUtils.join(",", errors.getErrors()));
            }
        });
}

LOGCAT 错误:

E/ERROR: File upload onError,File does not exist,Passed object is not file,Incorrect content type

我找到了解决方案。

每当您尝试添加 您的设备上实际不存在的文件时,就会出现此类错误。

为了解决这个问题,我只是下载了新文件,我的代码非常有效。

试试下面的方法 Code.It 可以上传图片、视频和音频

public void uploadAttachment(final File file, final int fileType) {

    String tags = CollectionsUtils.getMimeType(Uri.fromFile(file));
    ChatHelper.getInstance().loadFileAsAttachment(file,  tags, new QBEntityCallback<QBAttachment>() {
        @Override
        public void onSuccess(QBAttachment result, Bundle params) {
            fileUploadProgressMap.remove(item);
            result.setType(String.valueOf(fileType));
            fileQBAttachmentMap.put(item, result);
            notifyDataSetChanged();
        }

        @Override
        public void onError(QBResponseException e) {
            onAttachmentUploadErrorListener.onAttachmentUploadError(e);
            remove(item);
        }
    }, new QBProgressCallback() {
        @Override
        public void onProgressUpdate(final int progress) {
            fileUploadProgressMap.put(item, progress);
            mainThreadHandler.post(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();
                }
            });
        }
    });
}


 public void loadFileAsAttachment(File file, String tags, QBEntityCallback<QBAttachment> callback,
                                 QBProgressCallback progressCallback) {
    QBContent.uploadFileTask(file, true, tags, progressCallback)
            .performAsync(new QbEntityCallbackTwoTypeWrapper<QBFile, QBAttachment>(callback) {
                @Override
                public void onSuccess(QBFile qbFile, Bundle bundle) {
                    QBAttachment attachment = new QBAttachment(QBAttachment.PHOTO_TYPE);
                    attachment.setId(qbFile.getId().toString());
                    attachment.setUrl(qbFile.getPublicUrl());
                    callback.onSuccess(attachment, bundle);
                }
            });
}

宁愿使用 uri.getPath() 来获取文件,请使用 uri.getLastPathSegment()