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

3

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

  1. Selecting image from sd card. returns picture path in string and its not converting to inputStream. Have tried for about 10-15 answers on SOF. My code is as follows:

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

我尝试了以下代码以生成InputStream:

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

  2. try { is = IOUtils.toInputStream(picturePath, "UTF-8"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }

quickblox的内容面板显示该输出...其中size(kb)是我发送的静态试验图片大小...而1 kb大小是我选择的动态图片。

这段代码会在myfile文件夹中生成新的sample.jpg文件,但实际上并没有创建成功,反而出现了错误。但如果我选择静态图片,则代码能够完美地工作。

非常感谢您提前的帮助,我不知道我哪里做错了...非常感激任何形式的帮助。


你的目标版本是什么? - vojta
这可能是你的问题:https://dev59.com/Gl8d5IYBdhLWcg3wgya4 - vojta
@vojta...所以新文件系统限制了我进行修改?我会看一下的... - SK16
请参考此答案 https://dev59.com/tIjca4cB1Zd3GeqPsSf1#28739568 - Amrut Bidri
1个回答

2

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接