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