3
votes

I am developing a chatting app using Quickblox chat API And currently have done user registration,authentication and basic peer to peer chat and group chat. now implementing video,image and file sending But have stuck up on some part.

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

have tried these codes too for generating 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(); }

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.

the code generates new sample.jpg in myfile folder. It doesn't create. its broken. but if i choose statically then it works perfectly.

thank you in adv. don't know what i'm doing wrong...any help will be very much appreciable 2. I am

1
What is your target version?vojta
This might be your issue: stackoverflow.com/questions/26744842/…vojta
@vojta .... so new filesystem is restricting me from doing modifications? i'll hv a look...SK16

1 Answers

2
votes

after lots of efforts and code changing i done what i required. Instead of using inputstream, I used ByteArrayInputStream. Firstly converted my string picturepath to bitmap (don't know why i did that .. but did) ten converted that bitmap to ByteArrayInputStream using following code

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

and sent that result to my function....success..!!!