2015-03-02 271 views
2

我正在开发使用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); 
    

    保护无效onActivityResult(INT requestCode,INT发送resultCode,意图数据){ super.onActivityResult(requestCode,resultCode为,数据);

    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为=新ByteArrayInputStream的(。picturePath.toString()的getBytes(Charset.defaultCharset()));

  2. try { is = IOUtils.toInputStream(picturePath, "UTF-8"); } catch (IOException e1) { // TODO Auto-generated 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。它不创建。其破损。但如果我选择静态然后它完美的作品。

谢谢你在adv。不知道我在做什么错...任何帮助将非常明显 2.我

+0

你的目标版本是什么? – vojta 2015-03-02 09:41:08

+0

这可能是你的问题:http://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-lollipop – vojta 2015-03-02 09:42:48

+0

@vojta其21 ... – SK16 2015-03-02 10:25:40

回答

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

和结果发送到我的功能....成功。 。!