将文件(图像)从CacheDir复制到SD卡

Sam*_*ley 0 java android copy file

我希望能够从Android设备的内部缓存移动或复制文件,并将其放入SD卡的永久存储中.这是我到目前为止:

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();

}
Run Code Online (Sandbox Code Playgroud)

Vla*_*nov 8

/**
 * 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();
    }
}
Run Code Online (Sandbox Code Playgroud)