Skip to content

Instantly share code, notes, and snippets.

@imReker
Last active September 12, 2017 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imReker/a290ad691a596678c6efd0f9cf2f4e9c to your computer and use it in GitHub Desktop.
Save imReker/a290ad691a596678c6efd0f9cf2f4e9c to your computer and use it in GitHub Desktop.
[Android] Quick Copy/Extract files in assets to storage with java.nio
/*
Change the file extension of the files in assets that your want to copy to 'jpg'.
If you don't want change the extension, you can add
aaptOptions {
noCompress 'pdf' //Change to the extension of your assets files
}
to build.gradle.
*/
AssetFileDescriptor fd = null;
FileChannel outChannel = null;
FileChannel inChannel = null;
try {
fd = getAssets().openFd("AssetsFileName.pdf.jpg");
inChannel = fd.createInputStream().getChannel();
outChannel = new FileOutputStream("Destination.pdf").getChannel();
inChannel.transferTo(fd.getStartOffset(), fd.getLength(), outChannel);
} catch (IOException e) {
} finally {
try {
if (outChannel != null) outChannel.close();
if (inChannel != null) inChannel.close();
if (fd != null) fd.close();
} catch (IOException e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment