我正在開發使用Quickblox聊天API的聊天應用程序並且目前已完成用戶註冊,身份驗證和基本對等聊天和羣聊。現在正在實現視頻,圖像和文件發送但是有些部分已經出現。將字符串路徑轉換爲輸入流
從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
InputStream爲=新ByteArrayInputStream的(。picturePath.toString()的getBytes(Charset.defaultCharset()));
try { is = IOUtils.toInputStream(picturePath, "UTF-8"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
代碼生成MYFILE夾新sample.jpg。它不創建。其破損。但如果我選擇靜態然後它完美的作品。
謝謝你在adv。不知道我在做什麼錯...任何幫助將非常明顯 2.我
你的目標版本是什麼? – vojta 2015-03-02 09:41:08
這可能是你的問題:http://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-lollipop – vojta 2015-03-02 09:42:48
@vojta其21 ... – SK16 2015-03-02 10:25:40