android开发分享如何以编程方式移动,复制和删除SD上的文件和目录?

我想以编程方式移动,复制和删除SD卡上的文件和目录。 我已经做了Googlesearch,但找不到任何有用的东西。

    使用标准的Java I / O。 使用Environment.getExternalStorageDirectory()获取外部存储器的根目录(在某些设备上,它是SD卡)。

    在清单中设置正确的权限

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    下面是一个函数,将以编程方式移动您的文件

     private void moveFile(String inputPath, String inputFile, String outputPath) { InputStream in = null; OutputStream out = null; try { //create output directory if it doesn't exist File dir = new File (outputPath); if (!dir.exists()) { dir.mkdirs(); } in = new FileInputStream(inputPath + inputFile); out = new FileOutputStream(outputPath + inputFile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; // write the output file out.flush(); out.close(); out = null; // delete the original file new File(inputPath + inputFile).delete(); } catch (FileNotFoundException fnfe1) { Log.e("tag", fnfe1.getMessage()); } catch (Exception e) { Log.e("tag", e.getMessage()); } } 

    删除文件使用

     private void deleteFile(String inputPath, String inputFile) { try { // delete the original file new File(inputPath + inputFile).delete(); } catch (Exception e) { Log.e("tag", e.getMessage()); } } 

    复制

     private void copyFile(String inputPath, String inputFile, String outputPath) { InputStream in = null; OutputStream out = null; try { //create output directory if it doesn't exist File dir = new File (outputPath); if (!dir.exists()) { dir.mkdirs(); } in = new FileInputStream(inputPath + inputFile); out = new FileOutputStream(outputPath + inputFile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; // write the output file (You have now copied the file) out.flush(); out.close(); out = null; } catch (FileNotFoundException fnfe1) { Log.e("tag", fnfe1.getMessage()); } catch (Exception e) { Log.e("tag", e.getMessage()); } } 

    移动文件:

     File from = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic1/imagem.jpg"); File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic2/imagem.jpg"); from.renameTo(to); 

    移动文件的function:

     private void moveFile(File file, File dir) throws IOException { File newFile = new File(dir, file.getName()); FileChannel outputChannel = null; FileChannel inputChannel = null; try { outputChannel = new FileOutputStream(newFile).getChannel(); inputChannel = new FileInputStream(file).getChannel(); inputChannel.transferTo(0, inputChannel.size(), outputChannel); inputChannel.close(); file.delete(); } finally { if (inputChannel != null) inputChannel.close(); if (outputChannel != null) outputChannel.close(); } } 

    删除

     public static void deleteRecursive(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles()) deleteRecursive(child); fileOrDirectory.delete(); } 

    检查以上function的链接。

    复制

     public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation) throws IOException { if (sourceLocation.isDirectory()) { if (!targetLocation.exists()) { targetLocation.mkdir(); } String[] children = sourceLocation.list(); for (int i = 0; i < sourceLocation.listFiles().length; i++) { copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]), new File(targetLocation, children[i])); } } else { InputStream in = new FileInputStream(sourceLocation); OutputStream out = new FileOutputStream(targetLocation); // Copy the bits from instream to outstream byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } } 

    移动

    移动是没有什么只是复制文件夹的一个位置,然后删除文件夹

    performance

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

     File from = new File(Environment.getExternalStorageDirectory().getAbsolutePath().getAbsolutePath()+"/kaic1/imagem.jpg"); File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/kaic2/imagem.jpg"); from.renameTo(to); 

    如果你正在使用番石榴,你可以使用Files.move(source,dest)

     /** * Copy the local DB file of an application to the root of external storage directory * @param context the Context of application * @param dbName The name of the DB */ private void copyDbToExternalStorage(Context context , String dbName){ try { File name = context.getDatabasePath(dbName); File sdcardFile = new File(Environment.getExternalStorageDirectory() , "test.db");//The name of output file sdcardFile.createNewFile(); InputStream inputStream = null; OutputStream outputStream = null; inputStream = new FileInputStream(name); outputStream = new FileOutputStream(sdcardFile); byte[] buffer = new byte[1024]; int read; while ((read = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } inputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception e) { Log.e("Exception" , e.toString()); } } 

    使用Square的Okio复制文件:

     BufferedSink bufferedSink = Okio.buffer(Okio.sink(destinationFile)); bufferedSink.writeAll(Okio.source(sourceFile)); bufferedSink.close(); 

    Xamarin Android

     public static bool MoveFile(string CurrentFilePath, string NewFilePath) { try { using (var f = new File(CurrentFilePath)) using (var i = new FileInputStream(f)) using (var o = new FileOutputStream(NewFilePath)) { i.Channel.TransferTo(0, i.Channel.Size(), o.Channel); f.Delete(); } return true; } catch { return false; } } public static bool CopyFile(string CurrentFilePath, string NewFilePath) { try { using (var i = new FileInputStream(CurrentFilePath)) using (var o = new FileOutputStream(NewFilePath)) i.Channel.TransferTo(0, i.Channel.Size(), o.Channel); return true; } catch { return false; } } public static bool DeleteFile(string FilePath) { try { using (var file = new File(FilePath)) file.Delete(); return true; } catch { return false; } } 

    获取内部和外部SD卡path的path很简单。

    为此,我们只需要使用reflection方法,而不需要生根或签名的应用程序。 在清单中添加以下权限。

     uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" 

    码:

     final Class<?> surfaceControlClass = Class.forName("android.os.storage.StorageManager"); Method method1 = surfaceControlClass.getMethod("getVolumePaths") method1.setAccessible(true); final Object object = context.getSystemService(Context.STORAGE_SERVICE); String[] volumes = method1.invoke(object) 

      以上就是android开发分享如何以编程方式移动,复制和删除SD上的文件和目录?相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/520676.html

      (0)
      上一篇 2020年12月7日
      下一篇 2020年12月7日

      精彩推荐