Copy File (image) From Cachedir To Sd Card
I want to be able to either move or copy a file from the internal Cache of an android device and put this into permanent storage on the SD Card. This is what I have so far: public
Solution 1:
/**
* copy file from source to destination
*
* @param src source
* @param dst destination
* @throws java.io.IOException in case of any problems
*/voidcopyFile(File src, File dst)throws IOException {
FileChannelinChannel=newFileInputStream(src).getChannel();
FileChanneloutChannel=newFileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
Solution 2:
try this method
/**
* @param sourceLocation like this /mnt/sdcard/XXXX/XXXXX/15838e85-066d-4738-a243-76c461cd8b01.jpg
* @param destLocation /mnt/sdcard/XXXX/XXXXX/15838e85-066d-4738-a243-76c461cd8b01.jpg
* @return true if successful copy file and false othrerwise
*
* set this permissions in your application WRITE_EXTERNAL_STORAGE ,READ_EXTERNAL_STORAGE
*
*/publicstaticboolean copyFile(String sourceLocation, String destLocation) {
try {
File sd = Environment.getExternalStorageDirectory();
if(sd.canWrite()){
File source=new File(sourceLocation);
File dest=new File(destLocation);
if(!dest.exists()){
dest.createNewFile();
}
if(source.exists()){
InputStream src=new FileInputStream(source);
OutputStream dst=new FileOutputStream(dest);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = src.read(buf)) > 0) {
dst.write(buf, 0, len);
}
src.close();
dst.close();
}
}
returntrue;
} catch (Exception ex) {
ex.printStackTrace();
returnfalse;
}
}
for more info visit AndroidGuide
Post a Comment for "Copy File (image) From Cachedir To Sd Card"