1

I have the following setup: a gallery image gets selected using Intent.ACTION_GET_CONTENT intent and then in onActivityResult() I get an Intent with an Uri. Next I try to do the following:

Source source = Okio.source(getContentResolver().openInputStream(intent.getData()));
BufferedSink sink = Okio.buffer(Okio.sink(new File(outPath)));
long bytesWritten = sink.writeAll(source);

Here outPath is a valid path to existing 0-length file created in advance.

The copy operation completes without an error, bytesWritten return an actual bytes, which are same as the source file's size.

But when I do this afterwards:

BitmapFactory.decodeFile(outFile);

It returns null and produces a skia: decoder returned false log message. Which usually means that file's format is wrong.

Why is that? I also tried to do the same thing without using Okio (just writing a lot of ugly code which copies InputStream to OutputStream) and results were the same. Any hints?

Note, that the following works, but it has the downside that I have to additionally decode Bitmap. While I'd rather just copy InputStream to a file.

Bitmap b = BitmapFactory.decodeStream(getContentResolver().openInputStream(intent.getData()));
outStream = new FileOutputStream(outFile);
b.compress(Bitmap.CompressFormat.JPEG, 92, outStream);
dimsuz
  • 8,969
  • 8
  • 54
  • 88
  • and what is the content of `outFile` ? – pskink Jul 28 '15 at 09:28
  • @pskink hmm, just checked and it indeed looks strange, notice the unfinished strip at the bottom... So I guess for some reason it didn't flush() or what? http://i.imgur.com/8o4sQGO.jpg – dimsuz Jul 28 '15 at 10:38
  • 2
    yes, most likely flush() / close() problem – pskink Jul 28 '15 at 10:41
  • 1
    Oh, Indeed it seems like I didn't call close! Can you please submit this as an answer, I will accept. – dimsuz Jul 28 '15 at 10:42

0 Answers0