目录
1.前言
“明日复明日,明日何其多。 我生待明日,万事成蹉跎。 世人苦被明日累,春去秋来老将至。”
,
,
,时间有限,事情繁多,我们需要建立一个简单方法把它们分门别类地管理起来。
2.开发环境
android studio开发版本:
此app的开发模板:
3.主界面
4.菜单界面
5.复制粘贴界面
6.文件属性界面
7.重命名界面
8、部分代码
package com.fxyublib.android.seven.YubFile;
import java.io.File;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import androidx.core.content.FileProvider;
import com.fxyublib.android.seven.MyApplication;
import com.fxyublib.android.seven.R;
class FileOpenUtils {
private static boolean checkEndsWithInStringArray(String checkItsEnd, String[] fileEndings){
for(String aEnd : fileEndings){
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
static Intent getIntentByFile(Context context, File currentPath){
if(currentPath!=null && currentPath.isFile()){
String fileName = currentPath.toString().toLowerCase();
if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingImage))){
return getImageFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingWebText))){
return getHtmlFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingPackage))){
return getApkFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingAudio))){
return getAudioFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingVideo))){
return getVideoFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingText))){
return getTextFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingPdf))){
return getPdfFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingWord))){
return getWordFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingExcel))){
return getExcelFileIntent(currentPath);
}else if(checkEndsWithInStringArray(fileName, context.getResources().
getStringArray(R.array.fileEndingPPT))){
return getPPTFileIntent(currentPath);
}
}
return null;
}
private static Uri getFileUri(Intent intent, File file) {
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uri = FileProvider.getUriForFile(MyApplication.context(), MyApplication.context().getPackageName() + ".fileprovider", file);
//【content://{$authority}/external/temp.apk】或【content://{$authority}/files/bqt/temp2.apk】
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//【file:///storage/emulated/0/temp.apk】
uri = Uri.fromFile(file);
}
return uri;
}
//android获取一个用于打开HTML文件的intent
private static Intent getHtmlFileIntent(File file)
{
Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "text/html");
return intent;
}
//android获取一个用于打开图片文件的intent
private static Intent getImageFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "image/*");
return intent;
}
//android获取一个用于打开PDF文件的intent
private static Intent getPdfFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "application/pdf");
return intent;
}
//android获取一个用于打开文本文件的intent
private static Intent getTextFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "text/plain");
return intent;
}
//android获取一个用于打开音频文件的intent
private static Intent getAudioFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "audio/*");
return intent;
}
//android获取一个用于打开视频文件的intent
private static Intent getVideoFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "video/*");
return intent;
}
//android获取一个用于打开CHM文件的intent
private static Intent getChmFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
//android获取一个用于打开Word文件的intent
private static Intent getWordFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "application/msword");
return intent;
}
//android获取一个用于打开Excel文件的intent
private static Intent getExcelFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
//android获取一个用于打开PPT文件的intent
private static Intent getPPTFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}
//android获取一个用于打开apk文件的intent
private static Intent getApkFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = getFileUri(intent, file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
return intent;
}
}
后续
如果你觉得该方法或代码有一点点用处,可以给作者点个赞;╮( ̄▽ ̄)╭
如果你感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进。o_O???
谢谢各位童鞋们啦( ´ ▽ ` )ノ ( ´ ▽ ` )っ!!!