Copy file from uri | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Copy file from uri

In my app I have to copy a file selected by the user in my apps's folder. With a contenresolver I get an Inputstream of the uri, and I open a Fileoutputstream to my destination. After buffering both streams, I simply read each byte of the input and write it into the output. It works but the last round about 200 bytes get cut causing the file to get currupt. Jas anyone an idea what causes this? ps.: I'm executing this with an Asynctask.

2nd Jan 2017, 6:12 PM
No One
No One - avatar
1 Answer
+ 1
Close file handles? If this doesn't work, look for a 'flush' instruction. In languages that don't clean up for you (or async threads that get killed instead of shut down), last block data loss/garbage cluster is typical if your handles are open when you fall off the end of the program, especially when buffering.
27th May 2017, 2:21 AM
Kirk Schafer
Kirk Schafer - avatar