版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
封装了获取SD卡根目录路径、以及对文件读写、获取文件名等相关操作。
因为需要用到android.permission.READ_EXTERNAL_STORAGE权限,所以依赖《Android6.0运行时权限(基于RxPermission开源库)》。
效果图
代码分析
较常用的应该是获取SD卡根目录路径、获取文件名、创建目录操作。
使用步骤
一、项目组织结构图
注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
将fileutil包复制到项目中
创建一个包含以下代码的MyApplication.java(自定义的Application子类)
package com.why.project.fileutilsdemo; import android.app.Application;
import android.content.Context; /**
* Created by HaiyuKing
* Used 自定义Application【系统上下文】
*/ public class MyApplication extends Application {
/**系统上下文*/
private static Context mAppContext; @Override
public void onCreate() {
super.onCreate();
mAppContext = getApplicationContext();
} /**获取系统上下文:用于FileUtils工具类、Utility类*/
public static Context getAppContext()
{
return mAppContext;
} }
在AndroidManifest.xml中声明这个MyApplication并添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.why.project.fileutilsdemo"> <!-- ======================(FileUtil)========================== -->
<!-- 向SD卡写入数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".MyApplication">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application> </manifest>
添加运行时权限的处理(本demo中采用的是修改targetSDKVersion=22)
在build.gradle中导入第三方库:org.apache.httpcomponents:httpcore:4.4.4
apply plugin: 'com.android.application' android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.why.project.fileutilsdemo"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies { //FileUtils
compile 'org.apache.httpcomponents:httpcore:4.4.4' compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
对于Eclipse开发环境,在libs目录下导入httpcore-4.4.4.jar包即可。
链接:http://pan.baidu.com/s/1pKUzNiN 密码:xa8c
三、使用方法
private void initEvents() {
//获取SD卡路径
tv_getSdPath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sdPath = FileUtils.getSDPath();
Log.w(TAG,"sdPath="+sdPath);
}
}); //创建目录
tv_makeFolders.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fileForderPath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator;
Log.w(TAG,"fileForderPath="+fileForderPath);
boolean makeFolderState = FileUtils.makeFolders(fileForderPath);
Log.w(TAG,"makeFolderState="+makeFolderState);
}
}); //将字符串写入文件
tv_writeFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
String content = "将字符串内容添加到文本文件中";
boolean writeFileState = FileUtils.writeFile(filePath,content);
Log.w(TAG,"writeFileState="+writeFileState);
}
}); //读取文件
tv_readFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
String readContent = FileUtils.readFile(filePath);
Log.w(TAG,"readContent="+readContent);
}
}); //获取文件名(不带后缀)
tv_getFileNameWithoutExtension.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
String fileNameWithoutExtension = FileUtils.getFileNameWithoutExtension(filePath);
Log.w(TAG,"fileNameWithoutExtension="+fileNameWithoutExtension);
}
}); //获取文件名(带后缀)
tv_getFileName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
String fileName = FileUtils.getFileName(filePath);
Log.w(TAG,"fileName="+fileName);
}
}); //获取后缀名
tv_getFileExtension.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
String fileExtension = FileUtils.getFileExtension(filePath);
Log.w(TAG,"fileExtension="+fileExtension);
}
}); //获取文件大小
tv_getFileSize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
long fileSize = FileUtils.getFileSize(filePath);
Log.w(TAG,"fileSize="+fileSize);
}
}); //删除文件
tv_deleteFileRecursion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filePath = FileUtils.getSDPath() + File.separator + "fileUtilDir" + File.separator + "file.txt";
Log.w(TAG,"filePath="+filePath);
FileUtils.deleteFileRecursion(filePath); }
}); //获取APP的文件路径
tv_getAppFilePath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String appFilePath = FileUtils.getAppFilePath();
Log.w(TAG,"appFilePath="+appFilePath);
}
}); }
混淆配置
#=====================httpcore=====================
#android Studio环境中不需要,eclipse环境中需要
#-libraryjars libs/httpcore-4.4.4.jar
-dontwarn org.apache.http.**
-keep class org.apache.http.**{*;}