FileUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  3. * <p>
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. */
  9. package com.compdfkit.samples.util;
  10. import android.content.ActivityNotFoundException;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.net.Uri;
  14. import android.os.Build;
  15. import androidx.core.content.FileProvider;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. public class FileUtils {
  21. public static void shareFile(Context context, String title, String type, File file) {
  22. try {
  23. Intent intent = new Intent(Intent.ACTION_VIEW);
  24. intent.putExtra(Intent.EXTRA_SUBJECT, title);
  25. Uri uri = getUriBySystem(context, file);
  26. intent.putExtra(Intent.EXTRA_STREAM, uri);
  27. intent.setDataAndType(uri, type);
  28. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
  29. context.startActivity(Intent.createChooser(intent, title));
  30. } catch (ActivityNotFoundException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. public static Uri getUriBySystem(Context context, File file) {
  35. try {
  36. if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
  37. return Uri.fromFile(file);
  38. } else {
  39. return FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
  40. }
  41. } catch (Exception e) {
  42. return null;
  43. }
  44. }
  45. public static String getAssetsTempFile(Context context, String assetsName) {
  46. return copyFileFromAssets(context, assetsName, context.getCacheDir().getAbsolutePath(), assetsName, true);
  47. }
  48. public static String getNameWithoutExtension(String name){
  49. int index = name.lastIndexOf(".");
  50. return index == -1 ? name : name.substring(0, index);
  51. }
  52. public static String copyFileFromAssets(Context context,
  53. String assetName,
  54. String savePath,
  55. String saveName,
  56. final boolean overwriteExisting) {
  57. //if save path folder not exists, create directory
  58. File dir = new File(savePath);
  59. if (!dir.exists()) {
  60. if (!dir.mkdirs()) {
  61. return null;
  62. }
  63. }
  64. // 拷贝文件
  65. String filename = savePath + "/" + saveName;
  66. File file = new File(filename);
  67. if (file.exists()) {
  68. file.delete();
  69. }
  70. if (!file.exists() || overwriteExisting) {
  71. try {
  72. InputStream inStream = context.getAssets().open(assetName);
  73. FileOutputStream fileOutputStream = new FileOutputStream(filename);
  74. int byteread;
  75. byte[] buffer = new byte[1024];
  76. while ((byteread = inStream.read(buffer)) != -1) {
  77. fileOutputStream.write(buffer, 0, byteread);
  78. }
  79. fileOutputStream.flush();
  80. inStream.close();
  81. fileOutputStream.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. return file.getAbsolutePath();
  86. } else {
  87. return file.getAbsolutePath();
  88. }
  89. }
  90. public static void deleteFile(File file) {
  91. if (file.isDirectory()) {
  92. File[] files = file.listFiles();
  93. for (int i = 0; i < files.length; i++) {
  94. File f = files[i];
  95. deleteFile(f);
  96. }
  97. file.delete();//如要保留文件夹,只删除文件,请注释这行
  98. } else if (file.exists()) {
  99. file.delete();
  100. }
  101. }
  102. }