【发布时间】:2015-05-02 14:34:17
【问题描述】:
我正在使用 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);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 is = new 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。它不创造。它坏了。但如果我静态选择,那么它会完美运行。
谢谢你。不知道我做错了什么......任何帮助都会非常明显 2. 我是
【问题讨论】:
-
你的目标版本是什么?
-
@vojta .... 所以新的文件系统限制我进行修改?我去看看...
标签: java android string inputstream quickblox