gpt4 book ai didi

java - 将字符串路径转换为输入流

转载 作者:行者123 更新时间:2023-11-30 08:55:58 25 4
gpt4 key购买 nike

我正在使用 Quickblox 聊天 API 开发一个聊天应用程序,目前已经完成了用户注册、身份验证以及基本的点对点聊天和群聊。现在实现视频、图像和文件发送但是在某些部分卡住了。

  1. 正在从 SD 卡中选择图像。返回字符串中的图片路径,它不会转换为 inputStream。在 SOF 上尝试了大约 10-15 个答案。我的代码如下:

        Intent i = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, RESULT_LOAD_IMAGE);

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {


    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,
    filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();

    Bitmap imagebitmap=BitmapFactory.decodeFile(picturePath);

    byte[] b=picturePath.getBytes(Charset.forName("UTF-8"));

    InputStream is = new ByteArrayInputStream(b);

    File newFile = FileHelper.getFileInputStream(is, "sample.jpg", "myFile");

    Boolean fileIsPublic = false;

    QBContent.uploadFileTask(newFile, fileIsPublic, null, new QBEntityCallbackImpl<QBFile>()
    {
    public void onSuccess(QBFile file, Bundle params) {

    //creating message

    QBChatMessage chatmessage=new QBChatMessage();
    chatmessage.setProperty("save_to_history", "1"); //saves messsage to history

    QBAttachment qbAttachment=new QBAttachment("photo");
    qbAttachment.setId(file.getId().toString());

    chatmessage.addAttachment(qbAttachment);

    try
    {
    chat.sendMessage(chatmessage);

    Toast.makeText(getApplicationContext(), "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();

    } catch (XMPPException e) {
    Log.e(TAG, "failed to send a image", e);
    } catch (SmackException sme){
    Log.e(TAG, "failed to send a image", sme);
    }

    }

    public void onError(java.util.List<String> errors) {

    AlertDialog.Builder dialog = new AlertDialog.Builder(ChatActivity.this);
    dialog.setMessage("error when sending image: " + errors.toString()).create().show();


    };});}

也尝试过这些代码来生成输入流

  1. InputStream is = new ByteArrayInputStream(picturePath.toString().getBytes(Charset.defaultCharset()));

  2. 尝试{
    is = IOUtils.toInputStream(picturePath, "UTF-8");
    } catch (IOException e1){
    //TODO 自动生成的 catch block
    e1.printStackTrace();
    }

content panel of quickblox shows this output... where size(kb) refers to static trial images i sent...and 1 kb size is dynamic images i selected.

代码在 myfile 文件夹中生成新的 sample.jpg。它不创造。它坏了。但如果我选择静态,那么它就完美无缺。

谢谢你的副词。不知道我做错了什么......任何帮助都会非常明显2.我是

最佳答案

经过大量的努力和代码更改后,我完成了我需要的。我没有使用输入流,而是使用了 ByteArrayInputStream。首先将我的字符串 picturepath 转换为位图(不知道我为什么这样做......但是做了)十使用以下代码将该位图转换为 ByteArrayInputStream

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
imagebitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

并将结果发送到我的函数....成功..!!!

关于java - 将字符串路径转换为输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28806441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com