有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在安卓上开发quickblox消息附件功能

我想做的就是:OnViewClicked一个CreateChooser(type:images/*)将是开放的。 选择图像后,它将进入onActivityResult方法,在该方法中存储Uri。 发布正在调用的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


共 (3) 个答案

  1. # 1 楼答案

    试试下面的代码。我可以上传图像、视频和音频

    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);
                    }
                });
    }
    
  2. # 2 楼答案

    我找到了解决办法

    每当您试图添加设备上物理上不存在的文件时,就会显示此类错误

    为了解决这个问题,我下载了新文件&;我的代码很有魅力

  3. # 3 楼答案

    与其使用uri.getPath()获取文件,不如使用uri.getLastPathSegment()