0

After capturing an image, I'm unsure of how to upload the bitmap as a file. I get the bitmap this way:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
        if(requestCode == CAM_REQUEST_CODE){
             if(resultCode == RESULT_OK){
                  final Bitmap image = (Bitmap)data.getExtras().get("data");
             }
        }
    }

Using Dropbox API, a file is uploaded this way:

File file = new File("working-draft.txt");
FileInputStream inputStream = new FileInputStream(file);
Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream,
                            file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);

Any help on this is greatly appreciated.

user2892437
  • 1,111
  • 1
  • 14
  • 22
  • please have a look at http://stackoverflow.com/questions/15428975/save-bitmap-into-file-and-return-file-having-bitmap-image – Mithun May 27 '15 at 23:41
  • The `putFile` method takes an `InputStream` so if you can get the data into an `InputStream` directly, you can avoid writing it to a file in between. – Greg May 28 '15 at 00:02
  • It also takes file.length(), though. – user2892437 May 28 '15 at 00:13

1 Answers1

0

Here's one way you could do it:

  • Create a ByteArrayOutputStream
  • Call Bitmap.compress() to write the JPEG/PNG bytes to outputstream
  • Create a ByteArrayInputStream from the byte array you just output
  • Use that input stream for your putFile() method
  • Use the array size for your file length

I would be cautious about memory, though. It it was me, I'd try to find a way to return the bitmap source in the activity result instead of the actual bitmap.

kris larson
  • 30,387
  • 5
  • 62
  • 74