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

发布于 2024-11-07 21:51:13 字数 353 浏览 0 评论 0原文

我希望能够从 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();

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花海 2024-11-14 21:51:13
/**
 * 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();
    }
}
/**
 * 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();
    }
}
往日情怀 2024-11-14 21:51:13

尝试此方法

/**
* @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 
*  
*/
public static boolean 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();
            }
        }
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

以获取更多信息,请访问 Android指南

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 
*  
*/
public static boolean 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();
            }
        }
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

for more info visit AndroidGuide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文