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

Posted

技术标签:

【中文标题】quickbloxsdk:传递的对象不是文件,内容类型不正确?【英文标题】:quickbloxsdk : Passed object is not file,Incorrect content type? 【发布时间】:2017-01-04 12:20:51 【问题描述】:

我正在开发 android 中的 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: 文件上传错误,文件不存在,传递的对象不是文件,内容类型不正确

【问题讨论】:

【参考方案1】:

我找到了解决方案。

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

为了解决这个问题,我只是下载了新文件并且我的代码使用了一个魅力。

【讨论】:

您能否分享这些问题的示例代码。我必须从 camara/gallery 上传图片 @Pallavi 你到底想要什么?.. 我的意思是仅 uploadFileTask 或从galary + uploadFileTask 中选择文件? 我有相册部分,但我想同步两个 如果有帮助,请对此答案投票。编码快乐!【参考方案2】:

试试下面的代码。它适用于我上传图片、视频和音频

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);
                
            );

【讨论】:

【参考方案3】:

如果使用uri.getPath() 获取文件,请使用uri.getLastPathSegment()

【讨论】:

以上是关于quickbloxsdk:传递的对象不是文件,内容类型不正确?的主要内容,如果未能解决你的问题,请参考以下文章

如何传递 GOOGLE_APPLICATION_CREDENTIALS 中的内容而不是文件路径?

如何通过传递文件对象(而不是磁盘上文件的位置)将 ffmpeg 与 Python 一起使用

将const引用/指针传递给类进行存储的首选方法,而不是复制引用的对象

JAVA基础知识_对象

Java中的值传递和引用传递

Python对象的拷贝