Reputation: 2060
In Android, if I want to copy a file from one place to another:
How do I check that I have the correct Manifest permissions? I know you include the permissions in the manifest, but let's say we don't know that we included it or not -- how do we catch the exception if we lack the permission?
How can I verify that I have read access for the source file?
How can I verify that I have write access for the destination?
How can I verify that there is enough room to perform the write?
How can I verify that the write was performed successfully?
Upvotes: 0
Views: 34
Reputation: 1007399
how do we catch the exception if we lack the permission?
That varies by permission.
How can I verify that I have read access for the source file?
Call canRead()
on the File
.
How can I verify that I have write access for the destination?
Call canWrite()
on the File
.
How can I verify that there is enough room to perform the write?
You can't, insofar as by the time you have made the calculation, the value may have changed due to the behavior of other apps on the device.
You can use methods on StatFs
to determine the available space, but I would give yourself a significant buffer.
How can I verify that the write was performed successfully?
Start by calling getFD().sync()
on your FileOutputStream
, after flush()
and before close()
, to ensure all bytes are written to disk and are not being cached by the kernel and filesystem implementations. For most cases, that's sufficient.
You are also welcome to read the content back in and verify it against whatever you have in memory, to confirm that the file appears to contain what it should.
Upvotes: 1