问题描述
我希望能够从 android 设备的内部缓存中移动或复制文件,并将其放入 sd 卡上的永久存储中.这是我目前所拥有的:
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 void onclicksavesecret(view v){ file image = new file(getapplication().getcachedir() "/image.png"); file newimage = new file(environment.getexternalstoragedirectory() "/image.png"); toast.maketext(this, "image saved", 100).show(); }
推荐答案
/** * copy file from source to destination * * @param src source * @param dst destination * @throws java.io.ioexception in case of any problems */ void copyfile(file src, file dst) throws ioexception { filechannel inchannel = new fileinputstream(src).getchannel(); filechannel outchannel = new fileoutputstream(dst).getchannel(); try { inchannel.transferto(0, inchannel.size(), outchannel); } finally { if (inchannel != null) inchannel.close(); if (outchannel != null) outchannel.close(); } }