I have a few files in the assets
folder. I need to copy all of them to a folder say /sdcard/folder. I want to do this from within a thread. How do I do it?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Copy all files and directories from assets to your folder!
for copying better use apache commons io
//THIS IS MAIN METHOD FOR COPY
There are essentially two ways to do this.
First, you can use AssetManager.open and, as described by Rohith Nandakumar and iterate over the inputstream.
Second, you can use AssetManager.openFd, which allows you to use a FileChannel (which has the [transferTo](https://developer.android.com/reference/java/nio/channels/FileChannel.html#transferTo(long, long, java.nio.channels.WritableByteChannel)) and [transferFrom](https://developer.android.com/reference/java/nio/channels/FileChannel.html#transferFrom(java.nio.channels.ReadableByteChannel, long, long)) methods), so you don't have to loop over the input stream yourself.
I will describe the openFd method here.
Compression
First you need to ensure that the file is stored uncompressed. The packaging system may choose to compress any file with an extension that is not marked as noCompress, and compressed files cannot be memory mapped, so you will have to rely on AssetManager.open in that case.
You can add a '.mp3' extension to your file to stop it from being compressed, but the proper solution is to modify your app/build.gradle file and add the following lines (to disable compression of PDF files)
File packing
Note that the packager can still pack multiple files into one, so you can't just read the whole file the AssetManager gives you. You need to to ask the AssetFileDescriptor which parts you need.
Finding the correct part of the packed file
Once you've ensured your file is stored uncompressed, you can use the AssetManager.openFd method to obtain an AssetFileDescriptor, which can be used to obtain a FileInputStream (unlike AssetManager.open, which returns an InputStream) that contains a FileChannel. It also contains the starting offset (getStartOffset) and size (getLength), which you need to obtain the correct part of the file.
Implementation
An example implementation is given below:
This answer is based on JPM's answer.
I know this has been answered but I have a slightly more elegant way to copy from asset directory to a file on the sdcard. It requires no "for" loop but instead uses File Streams and Channels to do the work.
(Note) If using any type of compressed file, APK, PDF, ... you may want to rename the file extension before inserting into asset and then rename once you copy it to SDcard)
A way to copy a file without having to loop through it.
Here is a cleaned up version for current Android devices, functional method design so that you can copy it to an AssetsHelper class e.g ;)
Based on Yoram Cohen answer, here is a version that supports non static target directory.
Invoque with
copyFileOrDir(getDataDir(), "")
to write to internal app storage folder /data/data/pkg_name/Avoids copying "images" etc fake asset folders like
This is by far the best solution I have been able to find on the internet. I've used the following link https://gist.github.com/mhasby/026f02b33fcc4207b302a60645f6e217,
but it had a single error which I fixed and then it works like a charm. Here's my code. You can easily use it as it is an independent java class.
As you can see, just create an instance of
CopyAssets
in your java class which has an activity. Now this part is important, as far as my testing and researching on the internet,You cannot use AssetManager if the class has no activity
. It has something to do with the context of the java class.Now, the
c.copyAssets(getApplicationContext())
is an easy way to access the method, wherec
is and instance ofCopyAssets
class. As per my requirement, I allowed the program to copy all my resource files inside theasset
folder to the/www/resources/
of my internal directory.You can easily find out the part where you need to make changes to the directory as per your use. Feel free to ping me if you need any help.