package com.duke.mydownloadmanager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
/**
* @Author: duke
* @DateTime: 2017-05-25 10:31
* @Description: 手机内部、外部存储空间操作 version 2.0 <br/>
* <p>
* 1、context.getPackageCodePath():/data/app/your_app.apk
* 2、context.getPackageResourcePath():/data/app/your_app.apk
* 3、context.getDatabasePath("dk"):/data/data/<package name>/databases/dk
* 4、context.getDir("dk", Context.MODE_PRIVATE):/data/data/<package name>/app_dk
* 5、context.getCacheDir():/data/data/<package name>/cache
* 6、context.getFilesDir():/data/data/<package name>/files
* 7、context.getExternalFilesDir("dk"):/storage/sdcard0/Android/data/<package name>/files/dk
* 8、context.getExternalFilesDir(null):/storage/sdcard0/Android/data/<package name>/files
* 9、context.getExternalFilesDir():/storage/sdcard0/Android/data/<package name>/files -->"清除数据",随app下载而删除
* 10、context.getExternalCacheDir():/storage/sdcard0/Android/data/<package name>/cache -->"清除缓存",随app下载而删除
* <p/>
* 11、context.getLocalClassName():MainActivity
* <p/>
* 12、Environment.getRootDirectory():/system
* 13、Environment.getDataDirectory():/data
* 14、Environment.getDownloadCacheDirectory():/cache
* 15、Environment.getExternalStorageDirectory():/storage/sdcard0
* 16、Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS):/storage/sdcard0/Download
*/
public class FileUtils {
public static String getExtensionNameNoPoint(String path) {
return getExtensionNameNoPoint(newFile(path));
}
public static String getExtensionNameNoPoint(File file) {
if (file == null) {
return null;
}
String name = file.getName();
if ("".equals(name.trim())) {
return null;
}
if (!name.contains(".")) {
return null;
}
String[] arr = name.split("\\.");
return arr[arr.length - 1];
}
public static File newFile(String path) {
if (path == null || "".equals(path.trim())) {
return null;
}
return new File(path.trim());
}
public static boolean createFolderAndFile(File file) {
if (file == null) {
return false;
}
if (file.exists()) {
return true;
}
File parentFile = file.getParentFile();
if (parentFile == null) {
return false;
}
if (!parentFile.exists()) {
if (!parentFile.mkdirs()) {
return false;
}
}
try {
if (file.createNewFile()) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return false;
}
public static void deleteFiles(String path) {
deleteFiles(newFile(path));
}
/**
* 删除指定路径下的所有文件
*
* @param deleteFile parent文件对象
* @return 是否全部删除成功
*/
public static void deleteFiles(File deleteFile) {
if (deleteFile == null || !deleteFile.exists()) {
return;
}
if (deleteFile.isFile()) {
try {
deleteFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
} else {
File[] files = deleteFile.listFiles();
int size = files.length;
for (int i = 0; i < size; i++) {
deleteFiles(files[i]);
}
try {
//delete this empty directory
deleteFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static long getFileSize(String path) {
return getFileSize(newFile(path));
}
public static long getFileSize(File file) {
if (file == null || !file.exists()) {
return -1;
}
//数组是引用传递
long[] arr = {0};
countFile(file, arr);
return arr[0];
}
private static void countFile(File file, long[] totalArr) {
if (file == null || !file.exists()) {
return;
}
if (file.isFile()) {
totalArr[0] += file.length();
} else {
File[] files = file.listFiles();
int size = files.length;
for (int i = 0; i < size; i++) {
countFile(files[i], totalArr);
}
}
}
public static boolean renameFile(String oldPath, String newPath) {
return renameFile(newFile(oldPath), newFile(newPath));
}
public static boolean renameFile(File oldFile, File newFile) {
if (oldFile == null || !oldFile.exists()) {
return false;
}
if (newFile == null) {
return false;
}
if (!createFolderAndFile(newFile)) {
return false;
}
return oldFile.renameTo(newFile);
}
public static boolean saveFile(String data, String filePath) {
if (data == null || "".equals(data.trim())) {
return false;
}
return saveFile(data.getBytes(), filePath);
}
public static boolean saveFile(byte[] data, String filePath) {
if (data == null || data.length <= 0) {
return false;
}
File file = newFile(filePath);
if (!createFolderAndFile(file)) {
return false;
}
if (!file.canWrite()) {
return false;
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
fos = null;
}
}
}
return false;
}
public static boolean copyOnlyFile(File oldFile, File newFile) {
if (oldFile == null || !oldFile.exists()) {
return false;
}
if (oldFile.isDirectory()) {
return false;
}
if (newFile == null || newFile.isDirectory()) {
return false;
}
if (!createFolderAndFile(newFile)) {
return false;
}
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream(oldFile);
outputStream = new FileOutputStream(newFile);
byte[] buffer = new byte[8192];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
outputStream.flush();
}
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} fina
没有合适的资源?快使用搜索试试~ 我知道了~
android系统DownloadManager下载工具类
共79个文件
xml:38个
png:10个
java:9个
需积分: 14 26 下载量 198 浏览量
2017-06-18
10:59:02
上传
评论
收藏 265KB RAR 举报
温馨提示
android系统DownloadManager下载工具类,进行封装处理,简单易用。
资源推荐
资源详情
资源评论
收起资源包目录
MyDownloadManager.rar (79个子文件)
MyDownloadManager
gradlew.bat 2KB
gradlew 5KB
gradle.properties 747B
MyDownloadManager.iml 871B
.gradle
3.3
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.lock 17B
localClassSetAnalysis.bin 72KB
taskArtifacts
fileHashes.bin 134KB
taskArtifacts.bin 107KB
fileSnapshots.bin 2.74MB
taskArtifacts.lock 17B
app
proguard-rules.pro 949B
src
test
java
com
duke
mydownloadmanager
ExampleUnitTest.java 420B
androidTest
java
com
duke
mydownloadmanager
ExampleInstrumentedTest.java 782B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
values
colors.xml 214B
strings.xml 83B
styles.xml 394B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
layout
activity_main.xml 862B
java
com
duke
mydownloadmanager
FileUriPermissionCompat.java 4KB
StringUtils.java 10KB
FileUtils.java 20KB
MainActivity.java 3KB
MyDownloadManager.java 18KB
LogHelper.java 8KB
ApkManager.java 6KB
AndroidManifest.xml 980B
app.iml 10KB
.gitignore 8B
build.gradle 973B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 236B
.idea
gradle.xml 626B
runConfigurations.xml 564B
misc.xml 2KB
copyright
profiles_settings.xml 76B
modules.xml 371B
encodings.xml 159B
compiler.xml 686B
workspace.xml 178KB
libraries
javawriter_2_1_1.xml 330B
jsr305_2_0_1.xml 330B
support_annotations_25_3_1.xml 511B
support_v4_25_3_1.xml 404B
support_vector_drawable_25_3_1.xml 606B
rules_0_5.xml 548B
javax_annotation_api_1_2.xml 358B
runner_0_5.xml 551B
hamcrest_library_1_3.xml 342B
espresso_idling_resource_2_2_2.xml 620B
support_core_utils_25_3_1.xml 591B
animated_vector_drawable_25_3_1.xml 609B
constraint_layout_solver_1_0_2.xml 345B
hamcrest_core_1_3.xml 526B
junit_4_12.xml 477B
support_compat_25_3_1.xml 579B
support_core_ui_25_3_1.xml 582B
appcompat_v7_25_3_1.xml 573B
support_media_compat_25_3_1.xml 597B
constraint_layout_1_0_2.xml 410B
support_fragment_25_3_1.xml 585B
hamcrest_integration_1_3.xml 354B
espresso_core_2_2_2.xml 587B
javax_inject_1.xml 324B
exposed_instrumentation_api_publish_0_5.xml 638B
.gitignore 127B
local.properties 435B
settings.gradle 16B
build.gradle 521B
共 79 条
- 1
资源评论
android阿杜
- 粉丝: 424
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功