android - 列出特定文件夹(内部存储)中的文件并将所选文件移动到另一个文件夹
问题描述
内部存储中有一个名为“Projects”的文件夹,我想在列表视图中列出项目文件夹中可用的所有内容,然后用户选择的文件应重命名并复制到另一个名为“New Projects”的文件夹。请逐步解释在安卓。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case 7:
if (resultCode == RESULT_OK) {
String PathHolder = data.getData().getPath();
Toast.makeText(MockLocation.this, PathHolder, Toast.LENGTH_LONG).show();
String targetLocation = "/document/primary:HUACENAV/GnssServer/";
String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + PathHolder;
File source = new File(sourcePath);
String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + targetLocation;
File destination = new File(destinationPath);
copyFileOrDirectory(sourcePath,destinationPath);
}
break;
}
}
public static 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();
}
}
public static 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());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
解决方案
推荐阅读
- javascript - MongoError:池正在排出禁止的新操作
- c++ - 如何在多个源文件中使用静态成员结构?
- django - OneToOneField 在 django python3 或 fstring 或 self 属性问题中没有“用户名”成员
- swift - 关闭 SwiftUI 模式时触发一些代码
- recursion - 局部变量在递归调用的 Lotusscript 函数中是独立的吗?
- shopify - Shopify:如何收集有关我 Shopify 商店中列出的应用程序的统计信息?
- symfony - jms 序列化程序 @Exclude 类上的条件
- stenciljs - StencilJs 设置错字
- python - 大熊猫数据框管理
- python - 如何将 numpy 对象转换为常规 python 列表?