安卓异步复制文件对话框的实现,从手机移动整个文件夹到外置存储卡

本例实现了将APP的数据从手机内存移动到外置存储卡。

效果如下:



不看文章,接下载源代码戳这里


主要知识点如下:

1、获得APP的数据存储位置:

 

    File[] files = ContextCompat.getExternalFilesDirs(getBaseContext(), null);
File dirPhone = files[0];
File dirSDCard = files[1];

上面的files.length如果=2,那么files[0]是手机内存,files[1]则是外置存储卡。


2、扩展一个使用AsyncTask对象,进行异步复制操作,并提供进度显示。

 public class CopyTask extends AsyncTask<String, Integer, String> 
... ...

3、增加一个DialogFragment,作为弹出来的复制进度框(当然也可以使用DialogProgress,我这里使用DialogFragment的好处是自定义程度较高)。

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.copydialog_fragment, container, false);

        progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
        textViewMessage = (TextView) v.findViewById(R.id.textViewMessage);

        CopyTask copyTask = new CopyTask();
        copyTask.dirSourceString = getArguments().getString(CopyDialogFragment.SOURCE_FOLDER);
        copyTask.dirTargetString = getArguments().getString(CopyDialogFragment.TARGET_FOLDER);

        copyTask.execute();

        getDialog().setTitle("copying...");
        return v;
    }

4、主要几个递归复制的方法:

   private  void copyFileOrDirectory(String srcDir, String dstDir) {

            try {
                File src = new File(srcDir);
                File dst = new File(dstDir, src.getName());

                if (src.isDirectory()) {

                    String files[] = src.list();
                    int filesLength = files.length;
                    for (int i = 0; i < filesLength; i++) {
                        String src1 = (new File(src, files[i]).getPath());
                        String dst1 = dst.getPath();
                        copyFileOrDirectory(src1, dst1);

                    }
                } else {
                    copyFile(src, dst);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void copyFile(File sourceFile, File destFile) throws IOException {
            if (!destFile.getParentFile().exists())
                destFile.getParentFile().mkdirs();

            if (!destFile.exists()) {
                destFile.createNewFile();
            }

            FileChannel source = null;
            FileChannel destination = null;

            try {
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                destination.transferFrom(source, 0, source.size());
                progress += source.size();
                updateProgress(progress);
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (source != null) {
                    source.close();
                }
                if (destination != null) {
                    destination.close();
                }
            }
        }

好了,下面是详细内容:

CopyDialogFragment.java

package com.example.terry.storagesample;

import android.app.DialogFragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import j
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值