2015-03-02 285 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); 

和結果發送到我的功能....成功。 。!