1

I checked the past posts about the same issue (saveinbackground-doesnt-work).

I am trying to get the following code working with Parse.com:

sendMessage.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            setProgressBarIndeterminateVisibility(false);
            Log.d("Error","Completed Done");
            if (e==null){
                Toast.makeText(RecipientsActivity.this,"Message Sent!",Toast.LENGTH_LONG).show();
            } else {
                Log.d("TAG",e.getMessage());
                Log.d("TAG",e.getCode() + "error code");

            }
        }
    });

}

This saveinBackground method is taking too long to execute and when it does, it ends with giving the following error: i/o failure: java.net.SocketTimeoutException: null 100error code

I am using Parse in other pieces of the application too wherein I am fetching user contact from the Parse Core.

The message is the new class which is used to post pictures & video's on parse.com I am using the following code to create the ParseObject:

protected ParseObject createMessage(){
ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
    message.put(ParseConstants.KEY_SENDER_ID,ParseUser.getCurrentUser().getObjectId());
    message.put(ParseConstants.KEY_SENDER_NAME,ParseUser.getCurrentUser().getUsername());
    message.put(ParseConstants.KEY_RECIPIENTS_ID,getRecipientsIDs());
    message.put(ParseConstants.KEY_FILE_TYPE,fileType);
    //Log.d("TAG1","Message: " + message);
    //Log.d("TAG1",mediaUri + "");

    byte[] fileBytes = FileHelper.getByteArrayFromFile(this,mediaUri);

    if (fileBytes==null) return null;
    else {
            if (fileType.equals(ParseConstants.FILE_TYPE_IMAGE)){
            fileBytes = FileHelper.reduceImageForUpload(fileBytes);

     }
        String fileName = FileHelper.getFileName(this,mediaUri,fileType);
        ParseFile parseFile = new ParseFile(fileName,fileBytes);
        message.put(ParseConstants.KEY_FILE,parseFile);
       // Log.d("TAG2","Message: " + fileType);
       // Log.d("TAG2",mediaUri + "");
        return message;
    }
}

I checked the logs and found that the message is getting correctly created. Moreover I checked on Parse.com to see if any message class has been created there but none is being shown.

The error i am getting is this :

10-25 19:23:08.695  11405-11405 W/System.err﹕ com.parse.ParseException: Upload to S3 failed. Bad Request
10-25 19:23:08.695  11405-11405 W/System.err﹕ at com.parse.ParseAWSRequest.onResponse(ParseAWSRequest.java:94)
10-25 19:23:08.695  11405-11405/ W/System.err﹕ at com.parse.ParseAWSRequest.onResponse(ParseAWSRequest.java:28)
10-25 19:23:08.695  11405-11405/ W/System.err﹕ at com.parse.ParseRequest$3.call(ParseRequest.java:267)
10-25 19:23:08.695  11405-11405/ W/System.err﹕ at com.parse.Task$3.run(Task.java:199)
10-25 19:23:08.695  11405-11405/ W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-25 19:23:08.695  11405-11405/ W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-25 19:23:08.695  11405-11405/ W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

After trying to debug the error, I found that if I remove the code to attach the image, it works fine. So i tried various methods to convert image to Byte[] including image uri to byte array and saving files to parse but still i m not finding any luck

Community
  • 1
  • 1
nathandrake
  • 427
  • 1
  • 4
  • 19

1 Answers1

1

If someone else gets struck here:

I was having a conflict with the file name i was sending over to parse and the once i had given to the file when i was creating it due to which this problem was occurring.

Its a problem with converting the image to bytes and not an error on the parse side.

code to convert to bytes (following code contains fix for issues with imageview):

 public byte[] readBytes(Uri uri) throws IOException {
       /* // this dynamically extends to take the bytes you read
        InputStream inputStream = getContentResolver().openInputStream(uri);
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        // this is storage overwritten on each iteration with bytes
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }

        // and then we can return your byte array.
        return byteBuffer.toByteArray();

        */
        byte[] data = null;
        try {
            ContentResolver cr = getBaseContext().getContentResolver();
            InputStream inputStream = cr.openInputStream(uri);
            Bitmap bitmap;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;

            options.outHeight = 50;
            options.outWidth = 50;
            options.inSampleSize = 8;
            bitmap= BitmapFactory.decodeStream(inputStream,null,options);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            data = baos.toByteArray();
            bitmap.recycle();
            System.gc();
            Runtime.getRuntime().gc();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return data;
    }
nathandrake
  • 427
  • 1
  • 4
  • 19