├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── yis │ │ └── file │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── yis │ │ │ └── file │ │ │ ├── activity │ │ │ ├── FolderActivity.java │ │ │ ├── MainActivity.java │ │ │ └── MediaStoreActivity.java │ │ │ ├── adapter │ │ │ ├── FolderDataRecycleAdapter.java │ │ │ └── PublicTabViewPagerAdapter.java │ │ │ ├── fragment │ │ │ └── FolderDataFragment.java │ │ │ ├── model │ │ │ ├── FileInfo.java │ │ │ └── FolderInfo.java │ │ │ └── utils │ │ │ └── FileUtil.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_folder.xml │ │ ├── activity_main.xml │ │ ├── activity_media_store.xml │ │ ├── adapter_folder_data_rv_item.xml │ │ ├── adapter_image_rv_item.xml │ │ └── fragment_doc.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_round.png │ │ ├── image.png │ │ ├── pdf.png │ │ ├── ppt.png │ │ ├── word.png │ │ └── xls.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── yis │ └── file │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image ├── one.jpg ├── three.jpg └── two.jpg └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 文件选择器 3 | 4 | #### 效果图: 5 | 6 | | | | | 7 | |:------------------------------:|:---------------------------------:|:--------------------------------:| 8 | |![](image/one.jpg) | ![](image/two.jpg) | ![](image/three.jpg)| 9 | 10 |
11 | 分别使用遍历文件夹的方式和查询 Androd MediaStore多媒体库资源

12 | 13 | * [使用遍历文件夹:](#)
14 | 优点
15 |         可以实时拿到所要的数据
16 | 缺点:
17 |         效率相对比较低

18 | 19 | * [使用Android MediaStore:](#)
20 | 优点
21 |         效率比较高
22 | 缺点:
23 |         在通过第三方app下载之后,如果该app没有同步多媒体库,需要自己手动去同步多媒体库才能获取到实时的数据
24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.yis.file" 7 | minSdkVersion 15 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support:design:26.1.0' 25 | implementation 'com.github.bumptech.glide:glide:3.7.0' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/yis/file/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.yis.file; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.yis.file", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/activity/FolderActivity.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.activity; 2 | 3 | import android.app.ProgressDialog; 4 | import android.os.Bundle; 5 | import android.os.Environment; 6 | import android.os.Handler; 7 | import android.os.Message; 8 | import android.support.design.widget.TabLayout; 9 | import android.support.v4.app.Fragment; 10 | import android.support.v4.app.FragmentManager; 11 | import android.support.v4.view.ViewPager; 12 | import android.support.v7.app.AppCompatActivity; 13 | import android.util.Log; 14 | 15 | import com.yis.file.R; 16 | import com.yis.file.adapter.PublicTabViewPagerAdapter; 17 | import com.yis.file.fragment.FolderDataFragment; 18 | import com.yis.file.model.FileInfo; 19 | import com.yis.file.utils.FileUtil; 20 | 21 | import java.io.File; 22 | import java.util.ArrayList; 23 | import java.util.LinkedList; 24 | import java.util.List; 25 | 26 | /** 27 | * 使用遍历文件夹的方式 28 | */ 29 | public class FolderActivity extends AppCompatActivity { 30 | 31 | private TabLayout tlFile; 32 | private ViewPager vpFile; 33 | 34 | private List mTabTitle = new ArrayList<>(); 35 | private List mFragment = new ArrayList<>(); 36 | 37 | private ArrayList imageData = new ArrayList<>(); 38 | private ArrayList wordData = new ArrayList<>(); 39 | private ArrayList xlsData = new ArrayList<>(); 40 | private ArrayList pptData = new ArrayList<>(); 41 | private ArrayList pdfData = new ArrayList<>(); 42 | 43 | private ProgressDialog progressDialog; 44 | 45 | private Handler handler = new Handler() { 46 | @Override 47 | public void handleMessage(Message msg) { 48 | super.handleMessage(msg); 49 | if (msg.what == 1) { 50 | initData(); 51 | } 52 | } 53 | }; 54 | 55 | @Override 56 | protected void onDestroy() { 57 | super.onDestroy(); 58 | handler.removeMessages(1); 59 | } 60 | 61 | @Override 62 | protected void onCreate(Bundle savedInstanceState) { 63 | super.onCreate(savedInstanceState); 64 | setContentView(R.layout.activity_folder); 65 | 66 | tlFile = findViewById(R.id.tl_file); 67 | vpFile = findViewById(R.id.vp_file); 68 | 69 | 70 | progressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT); 71 | progressDialog.setMessage("正在加载中..."); 72 | progressDialog.setCanceledOnTouchOutside(false); 73 | progressDialog.show(); 74 | 75 | new Thread() { 76 | @Override 77 | public void run() { 78 | super.run(); 79 | getFolderData(); 80 | } 81 | }.start(); 82 | } 83 | 84 | /** 85 | * 遍历文件夹中资源 86 | */ 87 | public void getFolderData() { 88 | 89 | scanDirNoRecursion(Environment.getExternalStorageDirectory().toString()); 90 | handler.sendEmptyMessage(1); 91 | } 92 | 93 | 94 | private void initData() { 95 | 96 | mTabTitle = new ArrayList<>(); 97 | mFragment = new ArrayList<>(); 98 | 99 | mTabTitle.add("image"); 100 | mTabTitle.add("word"); 101 | mTabTitle.add("xls"); 102 | mTabTitle.add("ppt"); 103 | mTabTitle.add("pdf"); 104 | 105 | FolderDataFragment imageFragment = new FolderDataFragment(); 106 | Bundle imageBundle = new Bundle(); 107 | imageBundle.putParcelableArrayList("file_data", imageData); 108 | imageBundle.putBoolean("is_image", true); 109 | imageFragment.setArguments(imageBundle); 110 | mFragment.add(imageFragment); 111 | 112 | FolderDataFragment wordFragment = new FolderDataFragment(); 113 | Bundle wordBundle = new Bundle(); 114 | wordBundle.putParcelableArrayList("file_data", wordData); 115 | wordBundle.putBoolean("is_image", false); 116 | wordFragment.setArguments(wordBundle); 117 | mFragment.add(wordFragment); 118 | 119 | FolderDataFragment xlsFragment = new FolderDataFragment(); 120 | Bundle xlsBundle = new Bundle(); 121 | xlsBundle.putParcelableArrayList("file_data", xlsData); 122 | xlsBundle.putBoolean("is_image", false); 123 | xlsFragment.setArguments(xlsBundle); 124 | mFragment.add(xlsFragment); 125 | 126 | FolderDataFragment pptFragment = new FolderDataFragment(); 127 | Bundle pptBundle = new Bundle(); 128 | pptBundle.putParcelableArrayList("file_data", pptData); 129 | pptBundle.putBoolean("is_image", false); 130 | pptFragment.setArguments(pptBundle); 131 | mFragment.add(pptFragment); 132 | 133 | FolderDataFragment pdfFragment = new FolderDataFragment(); 134 | Bundle pdfBundle = new Bundle(); 135 | pdfBundle.putParcelableArrayList("file_data", pdfData); 136 | pdfBundle.putBoolean("is_image", false); 137 | pdfFragment.setArguments(pdfBundle); 138 | mFragment.add(pdfFragment); 139 | 140 | FragmentManager fragmentManager = getSupportFragmentManager(); 141 | 142 | PublicTabViewPagerAdapter tabViewPagerAdapter = new PublicTabViewPagerAdapter(fragmentManager, mFragment, mTabTitle); 143 | vpFile.setAdapter(tabViewPagerAdapter); 144 | 145 | tlFile.setupWithViewPager(vpFile); 146 | 147 | tlFile.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 148 | @Override 149 | public void onTabSelected(TabLayout.Tab tab) { 150 | vpFile.setCurrentItem(tab.getPosition()); 151 | } 152 | 153 | @Override 154 | public void onTabUnselected(TabLayout.Tab tab) { 155 | 156 | } 157 | 158 | @Override 159 | public void onTabReselected(TabLayout.Tab tab) { 160 | 161 | } 162 | }); 163 | 164 | progressDialog.dismiss(); 165 | } 166 | 167 | /** 168 | * 非递归 169 | * 170 | * @param path 171 | */ 172 | public void scanDirNoRecursion(String path) { 173 | LinkedList list = new LinkedList(); 174 | File dir = new File(path); 175 | File file[] = dir.listFiles(); 176 | if (file == null) return; 177 | for (int i = 0; i < file.length; i++) { 178 | if (file[i].isDirectory()) 179 | list.add(file[i]); 180 | else { 181 | System.out.println(file[i].getAbsolutePath()); 182 | } 183 | } 184 | File tmp; 185 | while (!list.isEmpty()) { 186 | tmp = (File) list.removeFirst();//首个目录 187 | if (tmp.isDirectory()) { 188 | file = tmp.listFiles(); 189 | if (file == null) 190 | continue; 191 | for (int i = 0; i < file.length; i++) { 192 | if (file[i].isDirectory()) 193 | list.add(file[i]);//目录则加入目录列表,关键 194 | else { 195 | // System.out.println(file[i]); 196 | if (file[i].getName().endsWith(".png") || file[i].getName().endsWith(".jpg") || file[i].getName().endsWith(".gif")) { 197 | //往图片集合中 添加图片的路径 198 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file[i].getAbsolutePath())); 199 | imageData.add(document); 200 | } else if (file[i].getName().endsWith(".doc") || file[i].getName().endsWith(".docx")) { 201 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file[i].getAbsolutePath())); 202 | wordData.add(document); 203 | } else if (file[i].getName().endsWith(".xls") || file[i].getName().endsWith(".xlsx")) { 204 | //往图片集合中 添加图片的路径 205 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file[i].getAbsolutePath())); 206 | xlsData.add(document); 207 | } else if (file[i].getName().endsWith(".ppt") || file[i].getName().endsWith(".pptx")) { 208 | //往图片集合中 添加图片的路径 209 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file[i].getAbsolutePath())); 210 | pptData.add(document); 211 | } else if (file[i].getName().endsWith(".pdf")) { 212 | //往图片集合中 添加图片的路径 213 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file[i].getAbsolutePath())); 214 | pdfData.add(document); 215 | } 216 | } 217 | } 218 | } else { 219 | System.out.println(tmp); 220 | } 221 | } 222 | } 223 | 224 | /** 225 | * 遍历手机所有文件 并将路径名存入集合中 参数需要 路径和集合 - 递归 226 | */ 227 | public void recursionFile(File dir) { 228 | //得到某个文件夹下所有的文件 229 | File[] files = dir.listFiles(); 230 | //文件为空 231 | if (files == null) { 232 | return; 233 | } 234 | //遍历当前文件下的所有文件 235 | for (File file : files) { 236 | //如果是文件夹 237 | if (file.isDirectory()) { 238 | //则递归(方法自己调用自己)继续遍历该文件夹 239 | recursionFile(file); 240 | } else { //如果不是文件夹 则是文件 241 | //如果文件名以 .mp3结尾则是mp3文件 242 | if (file.getName().endsWith(".doc") || file.getName().endsWith(".docx")) { 243 | //往图片集合中 添加图片的路径 244 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file.getAbsolutePath())); 245 | wordData.add(document); 246 | } else if (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx")) { 247 | //往图片集合中 添加图片的路径 248 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file.getAbsolutePath())); 249 | xlsData.add(document); 250 | } else if (file.getName().endsWith(".ppt") || file.getName().endsWith(".pptx")) { 251 | //往图片集合中 添加图片的路径 252 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file.getAbsolutePath())); 253 | pptData.add(document); 254 | } else if (file.getName().endsWith(".pdf")) { 255 | Log.i("qqq", "pdf======="); 256 | //往图片集合中 添加图片的路径 257 | FileInfo document = FileUtil.getFileInfoFromFile(new File(file.getAbsolutePath())); 258 | pdfData.add(document); 259 | } 260 | } 261 | } 262 | } 263 | } 264 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.activity; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | 9 | import com.yis.file.R; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | private Button btnFolder; 14 | private Button btnMedia; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | btnFolder = findViewById(R.id.btn_folder); 22 | btnMedia = findViewById(R.id.btn_media); 23 | 24 | btnFolder.setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View v) { 27 | Intent intent = new Intent(MainActivity.this,FolderActivity.class); 28 | startActivity(intent); 29 | } 30 | }); 31 | 32 | btnMedia.setOnClickListener(new View.OnClickListener() { 33 | @Override 34 | public void onClick(View v) { 35 | Intent intent = new Intent(MainActivity.this,MediaStoreActivity.class); 36 | startActivity(intent); 37 | } 38 | }); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/activity/MediaStoreActivity.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.activity; 2 | 3 | import android.app.ProgressDialog; 4 | import android.content.ContentResolver; 5 | import android.database.Cursor; 6 | import android.os.Bundle; 7 | import android.os.Environment; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.provider.MediaStore; 11 | import android.support.design.widget.TabLayout; 12 | import android.support.v4.app.Fragment; 13 | import android.support.v4.app.FragmentManager; 14 | import android.support.v4.app.LoaderManager; 15 | import android.support.v4.content.CursorLoader; 16 | import android.support.v4.content.Loader; 17 | import android.support.v4.view.ViewPager; 18 | import android.support.v7.app.AppCompatActivity; 19 | import android.text.TextUtils; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.widget.Button; 23 | 24 | import com.yis.file.R; 25 | import com.yis.file.adapter.PublicTabViewPagerAdapter; 26 | import com.yis.file.fragment.FolderDataFragment; 27 | import com.yis.file.model.FileInfo; 28 | import com.yis.file.utils.FileUtil; 29 | 30 | import java.io.File; 31 | import java.util.ArrayList; 32 | import java.util.Collections; 33 | import java.util.LinkedList; 34 | import java.util.List; 35 | 36 | /** 37 | * 使用 Media Store 多媒体库 38 | */ 39 | public class MediaStoreActivity extends AppCompatActivity { 40 | 41 | private TabLayout tlFile; 42 | private ViewPager vpFile; 43 | 44 | private List mTabTitle = new ArrayList<>(); 45 | private List mFragment = new ArrayList<>(); 46 | 47 | private ArrayList imageData = new ArrayList<>(); 48 | private ArrayList wordData = new ArrayList<>(); 49 | private ArrayList xlsData = new ArrayList<>(); 50 | private ArrayList pptData = new ArrayList<>(); 51 | private ArrayList pdfData = new ArrayList<>(); 52 | 53 | private ProgressDialog progressDialog; 54 | 55 | private Handler handler = new Handler() { 56 | @Override 57 | public void handleMessage(Message msg) { 58 | super.handleMessage(msg); 59 | if (msg.what == 1) { 60 | initData(); 61 | } 62 | } 63 | }; 64 | 65 | @Override 66 | protected void onDestroy() { 67 | super.onDestroy(); 68 | handler.removeMessages(1); 69 | } 70 | 71 | @Override 72 | protected void onCreate(Bundle savedInstanceState) { 73 | super.onCreate(savedInstanceState); 74 | setContentView(R.layout.activity_folder); 75 | 76 | tlFile = findViewById(R.id.tl_file); 77 | vpFile = findViewById(R.id.vp_file); 78 | 79 | 80 | progressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT); 81 | progressDialog.setMessage("正在加载中..."); 82 | progressDialog.setCanceledOnTouchOutside(false); 83 | progressDialog.show(); 84 | 85 | new Thread() { 86 | @Override 87 | public void run() { 88 | super.run(); 89 | getFolderData(); 90 | } 91 | }.start(); 92 | } 93 | 94 | /** 95 | * 遍历文件夹中资源 96 | */ 97 | public void getFolderData() { 98 | 99 | getImages(); 100 | 101 | getDocumentData(1); 102 | getDocumentData(2); 103 | getDocumentData(3); 104 | getDocumentData(4); 105 | 106 | handler.sendEmptyMessage(1); 107 | } 108 | 109 | 110 | private void initData() { 111 | 112 | mTabTitle = new ArrayList<>(); 113 | mFragment = new ArrayList<>(); 114 | 115 | mTabTitle.add("image"); 116 | mTabTitle.add("word"); 117 | mTabTitle.add("xls"); 118 | mTabTitle.add("ppt"); 119 | mTabTitle.add("pdf"); 120 | 121 | FolderDataFragment imageFragment = new FolderDataFragment(); 122 | Bundle imageBundle = new Bundle(); 123 | imageBundle.putParcelableArrayList("file_data", imageData); 124 | imageBundle.putBoolean("is_image", true); 125 | imageFragment.setArguments(imageBundle); 126 | mFragment.add(imageFragment); 127 | 128 | FolderDataFragment wordFragment = new FolderDataFragment(); 129 | Bundle wordBundle = new Bundle(); 130 | wordBundle.putParcelableArrayList("file_data", wordData); 131 | wordBundle.putBoolean("is_image", false); 132 | wordFragment.setArguments(wordBundle); 133 | mFragment.add(wordFragment); 134 | 135 | FolderDataFragment xlsFragment = new FolderDataFragment(); 136 | Bundle xlsBundle = new Bundle(); 137 | xlsBundle.putParcelableArrayList("file_data", xlsData); 138 | xlsBundle.putBoolean("is_image", false); 139 | xlsFragment.setArguments(xlsBundle); 140 | mFragment.add(xlsFragment); 141 | 142 | FolderDataFragment pptFragment = new FolderDataFragment(); 143 | Bundle pptBundle = new Bundle(); 144 | pptBundle.putParcelableArrayList("file_data", pptData); 145 | pptBundle.putBoolean("is_image", false); 146 | pptFragment.setArguments(pptBundle); 147 | mFragment.add(pptFragment); 148 | 149 | FolderDataFragment pdfFragment = new FolderDataFragment(); 150 | Bundle pdfBundle = new Bundle(); 151 | pdfBundle.putParcelableArrayList("file_data", pdfData); 152 | pdfBundle.putBoolean("is_image", false); 153 | pdfFragment.setArguments(pdfBundle); 154 | mFragment.add(pdfFragment); 155 | 156 | FragmentManager fragmentManager = getSupportFragmentManager(); 157 | 158 | PublicTabViewPagerAdapter tabViewPagerAdapter = new PublicTabViewPagerAdapter(fragmentManager, mFragment, mTabTitle); 159 | vpFile.setAdapter(tabViewPagerAdapter); 160 | 161 | tlFile.setupWithViewPager(vpFile); 162 | 163 | tlFile.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 164 | @Override 165 | public void onTabSelected(TabLayout.Tab tab) { 166 | vpFile.setCurrentItem(tab.getPosition()); 167 | } 168 | 169 | @Override 170 | public void onTabUnselected(TabLayout.Tab tab) { 171 | 172 | } 173 | 174 | @Override 175 | public void onTabReselected(TabLayout.Tab tab) { 176 | 177 | } 178 | }); 179 | 180 | progressDialog.dismiss(); 181 | } 182 | 183 | /** 184 | * 加载图片 185 | */ 186 | public void getImages() { 187 | 188 | String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DISPLAY_NAME}; 189 | 190 | //asc 按升序排列 191 | //desc 按降序排列 192 | //projection 是定义返回的数据,selection 通常的sql 语句,例如 selection=MediaStore.Images.ImageColumns.MIME_TYPE+"=? " 那么 selectionArgs=new String[]{"jpg"}; 193 | ContentResolver mContentResolver = this.getContentResolver(); 194 | Cursor cursor = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_MODIFIED + " desc"); 195 | 196 | 197 | String imageId = null; 198 | 199 | String fileName; 200 | 201 | String filePath; 202 | 203 | while (cursor.moveToNext()) { 204 | 205 | imageId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)); 206 | 207 | fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)); 208 | 209 | filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)); 210 | 211 | 212 | Log.e("photo", imageId + " -- " + fileName + " -- " + filePath); 213 | FileInfo fileInfo = FileUtil.getFileInfoFromFile(new File(filePath)); 214 | imageData.add(fileInfo); 215 | } 216 | cursor.close(); 217 | 218 | cursor = null; 219 | } 220 | 221 | /** 222 | * 获取手机文档数据 223 | * 224 | * @param selectType 225 | */ 226 | public void getDocumentData(int selectType) { 227 | 228 | String[] columns = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.MIME_TYPE, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.DATA}; 229 | 230 | String select = ""; 231 | 232 | switch (selectType) { 233 | //word 234 | case 1: 235 | select = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%.doc'" + " or " + MediaStore.Files.FileColumns.DATA + " LIKE '%.docx'" + ")"; 236 | break; 237 | //xls 238 | case 2: 239 | select = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%.xls'" + " or " + MediaStore.Files.FileColumns.DATA + " LIKE '%.xlsx'" + ")"; 240 | break; 241 | //ppt 242 | case 3: 243 | select = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%.ppt'" + " or " + MediaStore.Files.FileColumns.DATA + " LIKE '%.pptx'" + ")"; 244 | break; 245 | //pdf 246 | case 4: 247 | select = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%.pdf'" + ")"; 248 | break; 249 | } 250 | 251 | // List dataList = new ArrayList<>(); 252 | ContentResolver contentResolver = getContentResolver(); 253 | Cursor cursor = contentResolver.query(MediaStore.Files.getContentUri("external"), columns, select, null, null); 254 | // Cursor cursor = contentResolver.query(Uri.parse(Environment.getExternalStorageDirectory() + "/tencent/QQfile_recv/"), columns, select, null, null); 255 | 256 | int columnIndexOrThrow_DATA = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA); 257 | 258 | if (cursor != null) { 259 | while (cursor.moveToNext()) { 260 | 261 | String path = cursor.getString(columnIndexOrThrow_DATA); 262 | 263 | FileInfo document = FileUtil.getFileInfoFromFile(new File(path)); 264 | 265 | // dataList.add(document); 266 | switch (selectType) { 267 | //word 268 | case 1: 269 | wordData.add(document); 270 | break; 271 | //xls 272 | case 2: 273 | xlsData.add(document); 274 | break; 275 | //ppt 276 | case 3: 277 | pptData.add(document); 278 | break; 279 | //pdf 280 | case 4: 281 | pdfData.add(document); 282 | break; 283 | } 284 | } 285 | cursor.close(); 286 | } 287 | } 288 | 289 | 290 | } 291 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/adapter/FolderDataRecycleAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.adapter; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.RelativeLayout; 10 | import android.widget.TextView; 11 | 12 | import com.bumptech.glide.Glide; 13 | import com.yis.file.R; 14 | import com.yis.file.model.FileInfo; 15 | import com.yis.file.utils.FileUtil; 16 | 17 | import java.util.List; 18 | 19 | 20 | /** 21 | * 使用遍历文件夹的方式 22 | * Created by yis on 2018/4/17. 23 | */ 24 | 25 | public class FolderDataRecycleAdapter extends RecyclerView.Adapter { 26 | 27 | private Context mContext; 28 | private List data; 29 | private boolean isPhoto = false; 30 | 31 | public FolderDataRecycleAdapter(Context mContext, List data, boolean isPhoto) { 32 | this.mContext = mContext; 33 | this.data = data; 34 | this.isPhoto = isPhoto; 35 | } 36 | 37 | @Override 38 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 39 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_folder_data_rv_item, parent, false); 40 | ViewHolder viewHolder = new ViewHolder(view); 41 | return viewHolder; 42 | } 43 | 44 | @Override 45 | public void onBindViewHolder(final ViewHolder holder, final int position) { 46 | 47 | holder.tv_content.setText(data.get(position).getFileName()); 48 | holder.tv_size.setText(FileUtil.FormetFileSize(data.get(position).getFileSize())); 49 | holder.tv_time.setText(data.get(position).getTime()); 50 | 51 | //封面图 52 | if (isPhoto) { 53 | Glide.with(mContext).load(data.get(position).getFilePath()).into(holder.iv_cover); 54 | } else { 55 | Glide.with(mContext).load(FileUtil.getFileTypeImageId(mContext, data.get(position).getFilePath())).fitCenter().into(holder.iv_cover); 56 | } 57 | } 58 | 59 | @Override 60 | public int getItemCount() { 61 | return data.size(); 62 | } 63 | 64 | public static class ViewHolder extends RecyclerView.ViewHolder { 65 | 66 | RelativeLayout rlMain; 67 | TextView tv_content; 68 | TextView tv_size; 69 | TextView tv_time; 70 | ImageView iv_cover; 71 | 72 | public ViewHolder(View itemView) { 73 | super(itemView); 74 | rlMain = itemView.findViewById(R.id.rl_main); 75 | tv_content = itemView.findViewById(R.id.tv_content); 76 | tv_size = itemView.findViewById(R.id.tv_size); 77 | tv_time = itemView.findViewById(R.id.tv_time); 78 | iv_cover = itemView.findViewById(R.id.iv_cover); 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/adapter/PublicTabViewPagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.adapter; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentManager; 5 | import android.support.v4.app.FragmentPagerAdapter; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 选项卡tab 11 | * Created by yis on 2018/4/17. 12 | */ 13 | public class PublicTabViewPagerAdapter extends FragmentPagerAdapter { 14 | 15 | private List mFragments;//fragment列表 16 | private List mTitles;//tab名的列表 17 | 18 | public PublicTabViewPagerAdapter(FragmentManager fm, List list_fragment, List list_Title) { 19 | super(fm); 20 | this.mFragments = list_fragment; 21 | this.mTitles = list_Title; 22 | } 23 | 24 | @Override 25 | public Fragment getItem(int i) { 26 | return mFragments.get(i); 27 | } 28 | 29 | @Override 30 | public int getCount() { 31 | return mFragments.size(); 32 | } 33 | 34 | @Override 35 | public CharSequence getPageTitle(int position) { 36 | return mTitles.get(position % mTitles.size()); 37 | } 38 | 39 | 40 | // @Override 41 | // public void destroyItem(ViewGroup container, int position, Object object) { 42 | //// viewpager不会被销毁 43 | // super.destroyItem(container, position, object); 44 | // } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/fragment/FolderDataFragment.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | import com.yis.file.R; 13 | import com.yis.file.adapter.FolderDataRecycleAdapter; 14 | import com.yis.file.model.FileInfo; 15 | 16 | import java.util.List; 17 | 18 | /** 19 | * Created by yis on 2018/4/17. 20 | */ 21 | 22 | public class FolderDataFragment extends Fragment { 23 | 24 | private RecyclerView rvDoc; 25 | 26 | @Nullable 27 | @Override 28 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 29 | View rootView = inflater.inflate(R.layout.fragment_doc, container, false); 30 | rvDoc = rootView.findViewById(R.id.rv_doc); 31 | return rootView; 32 | } 33 | 34 | @Override 35 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 36 | super.onActivityCreated(savedInstanceState); 37 | 38 | initData(); 39 | } 40 | 41 | private void initData() { 42 | Bundle bundle = this.getArguments(); 43 | 44 | List data = bundle.getParcelableArrayList("file_data"); 45 | boolean isImage = bundle.getBoolean("is_image"); 46 | 47 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); 48 | //设置RecyclerView 布局 49 | rvDoc.setLayoutManager(linearLayoutManager); 50 | FolderDataRecycleAdapter pptListAdapter = new FolderDataRecycleAdapter(getActivity(), data, isImage); 51 | rvDoc.setAdapter(pptListAdapter); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/model/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.model; 2 | 3 | 4 | import android.os.Parcel; 5 | import android.os.Parcelable; 6 | 7 | /** 8 | * Created by yis on 2018/4/17. 9 | */ 10 | public class FileInfo implements Parcelable { 11 | 12 | private String fileName; 13 | private String filePath; 14 | private long fileSize; 15 | private String time; 16 | 17 | public String getFilePath() { 18 | return filePath == null ? "" : filePath; 19 | } 20 | 21 | public void setFilePath(String filePath) { 22 | this.filePath = filePath; 23 | } 24 | 25 | public String getFileName() { 26 | return fileName == null ? "" : fileName; 27 | } 28 | 29 | public void setFileName(String fileName) { 30 | this.fileName = fileName; 31 | } 32 | 33 | public long getFileSize() { 34 | return fileSize; 35 | } 36 | 37 | public void setFileSize(long fileSize) { 38 | this.fileSize = fileSize; 39 | } 40 | 41 | public String getTime() { 42 | return time == null ? "" : time; 43 | } 44 | 45 | public void setTime(String time) { 46 | this.time = time; 47 | } 48 | 49 | 50 | @Override 51 | public int describeContents() { 52 | return 0; 53 | } 54 | 55 | @Override 56 | public void writeToParcel(Parcel dest, int flags) { 57 | dest.writeString(this.fileName); 58 | dest.writeString(this.filePath); 59 | dest.writeLong(this.fileSize); 60 | dest.writeString(this.time); 61 | } 62 | 63 | public FileInfo() { 64 | } 65 | 66 | protected FileInfo(Parcel in) { 67 | this.fileName = in.readString(); 68 | this.filePath = in.readString(); 69 | this.fileSize = in.readLong(); 70 | this.time = in.readString(); 71 | } 72 | 73 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 74 | @Override 75 | public FileInfo createFromParcel(Parcel source) { 76 | return new FileInfo(source); 77 | } 78 | 79 | @Override 80 | public FileInfo[] newArray(int size) { 81 | return new FileInfo[size]; 82 | } 83 | }; 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/model/FolderInfo.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by yis on 2018/4/17. 8 | */ 9 | 10 | public class FolderInfo { 11 | private String name; 12 | private String path; 13 | private List images = new ArrayList(); 14 | 15 | public List getImages() { 16 | return images; 17 | } 18 | 19 | public void setImages(List images) { 20 | this.images = images; 21 | } 22 | 23 | public String getPath() { 24 | return path; 25 | } 26 | 27 | public void setPath(String path) { 28 | this.path = path; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/yis/file/utils/FileUtil.java: -------------------------------------------------------------------------------- 1 | package com.yis.file.utils; 2 | 3 | import android.content.Context; 4 | import android.os.storage.StorageManager; 5 | 6 | import com.yis.file.R; 7 | import com.yis.file.model.FileInfo; 8 | 9 | import java.io.File; 10 | import java.io.FileFilter; 11 | import java.lang.reflect.Array; 12 | import java.lang.reflect.InvocationTargetException; 13 | import java.lang.reflect.Method; 14 | import java.text.DecimalFormat; 15 | import java.text.SimpleDateFormat; 16 | import java.util.ArrayList; 17 | import java.util.Calendar; 18 | import java.util.Collections; 19 | import java.util.Comparator; 20 | import java.util.Date; 21 | import java.util.List; 22 | 23 | 24 | /** 25 | * Created by CWJ on 2017/3/20. 26 | */ 27 | 28 | public class FileUtil { 29 | 30 | /**** 31 | * 计算文件大小 32 | * 33 | * @param length 34 | * @return 35 | */ 36 | public static String getFileSzie(Long length) { 37 | if (length >= 1048576) { 38 | return (length / 1048576) + "MB"; 39 | } else if (length >= 1024) { 40 | return (length / 1024) + "KB"; 41 | } else if (length < 1024) { 42 | return length + "B"; 43 | } else { 44 | return "0KB"; 45 | } 46 | } 47 | 48 | public static String FormetFileSize(long fileS) { 49 | DecimalFormat df = new DecimalFormat("#.00"); 50 | String fileSizeString = ""; 51 | String wrongSize = "0B"; 52 | if (fileS == 0) { 53 | return wrongSize; 54 | } 55 | if (fileS < 1024) { 56 | fileSizeString = df.format((double) fileS) + "B"; 57 | } else if (fileS < 1048576) { 58 | fileSizeString = df.format((double) fileS / 1024) + "KB"; 59 | } else if (fileS < 1073741824) { 60 | fileSizeString = df.format((double) fileS / 1048576) + "MB"; 61 | } else { 62 | fileSizeString = df.format((double) fileS / 1073741824) + "GB"; 63 | } 64 | return fileSizeString; 65 | } 66 | 67 | /** 68 | * 字符串时间戳转时间格式 69 | * 70 | * @param timeStamp 71 | * @return 72 | */ 73 | public static String getStrTime(String timeStamp) { 74 | String timeString = null; 75 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm"); 76 | long l = Long.valueOf(timeStamp) * 1000; 77 | timeString = sdf.format(new Date(l)); 78 | return timeString; 79 | } 80 | 81 | /** 82 | * 读取文件的最后修改时间的方法 83 | */ 84 | public static String getFileLastModifiedTime(File f) { 85 | Calendar cal = Calendar.getInstance(); 86 | long time = f.lastModified(); 87 | SimpleDateFormat formatter = new 88 | SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 89 | cal.setTimeInMillis(time); 90 | return formatter.format(cal.getTime()); 91 | } 92 | 93 | /** 94 | * 获取扩展内存的路径 95 | * 96 | * @param mContext 97 | * @return 98 | */ 99 | public static String getStoragePath(Context mContext) { 100 | 101 | StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); 102 | Class storageVolumeClazz = null; 103 | try { 104 | storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); 105 | Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); 106 | Method getPath = storageVolumeClazz.getMethod("getPath"); 107 | Method isRemovable = storageVolumeClazz.getMethod("isRemovable"); 108 | Object result = getVolumeList.invoke(mStorageManager); 109 | final int length = Array.getLength(result); 110 | for (int i = 0; i < length; i++) { 111 | Object storageVolumeElement = Array.get(result, i); 112 | String path = (String) getPath.invoke(storageVolumeElement); 113 | boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement); 114 | if (removable) { 115 | return path; 116 | } 117 | } 118 | } catch (ClassNotFoundException e) { 119 | e.printStackTrace(); 120 | } catch (InvocationTargetException e) { 121 | e.printStackTrace(); 122 | } catch (NoSuchMethodException e) { 123 | e.printStackTrace(); 124 | } catch (IllegalAccessException e) { 125 | e.printStackTrace(); 126 | } 127 | return null; 128 | } 129 | 130 | public static int getFileTypeImageId(Context mContext, String fileName) { 131 | int id; 132 | if (checkSuffix(fileName, new String[]{"doc", "docx"})) { 133 | id = R.mipmap.word; 134 | } else if (checkSuffix(fileName, new String[]{"ppt", "pptx"})) { 135 | id = R.mipmap.ppt; 136 | } else if (checkSuffix(fileName, new String[]{"xls", "xlsx"})) { 137 | id = R.mipmap.xls; 138 | } else if (checkSuffix(fileName, new String[]{"pdf"})) { 139 | id = R.mipmap.pdf; 140 | } else 141 | id = R.mipmap.image; 142 | return id; 143 | } 144 | 145 | public static boolean checkSuffix(String fileName, 146 | String[] fileSuffix) { 147 | for (String suffix : fileSuffix) { 148 | if (fileName != null) { 149 | if (fileName.toLowerCase().endsWith(suffix)) { 150 | return true; 151 | } 152 | } 153 | } 154 | return false; 155 | } 156 | 157 | /** 158 | * 文件过滤,将手机中隐藏的文件给过滤掉 159 | */ 160 | public static File[] fileFilter(File file) { 161 | File[] files = file.listFiles(new FileFilter() { 162 | 163 | @Override 164 | public boolean accept(File pathname) { 165 | return !pathname.isHidden(); 166 | } 167 | }); 168 | return files; 169 | } 170 | 171 | 172 | // public static List getFilesInfo(List fileDir, Context mContext) { 173 | // List mlist = new ArrayList<>(); 174 | // for (int i = 0; i < fileDir.size(); i++) { 175 | // if (new File(fileDir.get(i)).exists()) { 176 | // mlist = FilesInfo(new File(fileDir.get(i)), mContext); 177 | // } 178 | // } 179 | // return mlist; 180 | // } 181 | 182 | private static List FilesInfo(File fileDir, Context mContext) { 183 | List videoFilesInfo = new ArrayList<>(); 184 | File[] listFiles = fileFilter(fileDir); 185 | if (listFiles != null) { 186 | for (File file : listFiles) { 187 | if (file.isDirectory()) { 188 | FilesInfo(file, mContext); 189 | } else { 190 | FileInfo fileInfo = getFileInfoFromFile(file); 191 | videoFilesInfo.add(fileInfo); 192 | } 193 | } 194 | } 195 | return videoFilesInfo; 196 | } 197 | 198 | public static List getFileInfosFromFileArray(File[] files) { 199 | List fileInfos = new ArrayList<>(); 200 | for (File file : files) { 201 | FileInfo fileInfo = getFileInfoFromFile(file); 202 | fileInfos.add(fileInfo); 203 | } 204 | Collections.sort(fileInfos, new FileNameComparator()); 205 | return fileInfos; 206 | } 207 | 208 | /** 209 | * 根据文件名进行比较排序 210 | */ 211 | public static class FileNameComparator implements Comparator { 212 | protected final static int 213 | FIRST = -1, 214 | SECOND = 1; 215 | 216 | @Override 217 | public int compare(FileInfo lhs, FileInfo rhs) { 218 | // if (lhs.isDirectory() || rhs.isDirectory()) { 219 | // if (lhs.isDirectory() == rhs.isDirectory()) 220 | // return lhs.getFileName().compareToIgnoreCase(rhs.getFileName()); 221 | // else if (lhs.isDirectory()) return FIRST; 222 | // else return SECOND; 223 | // } 224 | return lhs.getFileName().compareToIgnoreCase(rhs.getFileName()); 225 | } 226 | } 227 | 228 | // 229 | public static FileInfo getFileInfoFromFile(File file) { 230 | FileInfo fileInfo = new FileInfo(); 231 | fileInfo.setFileName(file.getName()); 232 | fileInfo.setFilePath(file.getPath()); 233 | fileInfo.setFileSize(file.length()); 234 | // fileInfo.setDirectory(file.isDirectory()); 235 | fileInfo.setTime(FileUtil.getFileLastModifiedTime(file)); 236 | int lastDotIndex = file.getName().lastIndexOf("."); 237 | if (lastDotIndex > 0) { 238 | String fileSuffix = file.getName().substring(lastDotIndex + 1); 239 | // fileInfo.setSuffix(fileSuffix); 240 | } 241 | return fileInfo; 242 | } 243 | // 244 | // public static List queryFolderInfo(Context context, List mlist) { 245 | // List folderInfos = new ArrayList<>(); 246 | // 247 | // for (int i = 0; i < mlist.size(); i++) { 248 | // String[] projection = new String[]{ 249 | // MediaStore.Files.FileColumns._ID, 250 | // MediaStore.Files.FileColumns.DATA, 251 | // MediaStore.Files.FileColumns.DISPLAY_NAME, 252 | // MediaStore.Files.FileColumns.DATE_MODIFIED 253 | // }; 254 | // Cursor cursor = context.getContentResolver().query( 255 | // mlist.get(i), 256 | // projection, null, 257 | // null, projection[2] + " DESC"); 258 | // 259 | // if (cursor != null) { 260 | // if (cursor.moveToFirst()) { 261 | // int dataindex = cursor 262 | // .getColumnIndex(MediaStore.Files.FileColumns.DATA); 263 | // int nameindex = cursor 264 | // .getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME); 265 | // int timeindex = cursor 266 | // .getColumnIndex(MediaStore.Files.FileColumns.DATE_MODIFIED); 267 | // do { 268 | // FileInfo fileInfo = new FileInfo(); 269 | // String path = cursor.getString(dataindex); 270 | // String name = cursor.getString(nameindex); 271 | // String time = cursor.getString(timeindex); 272 | // fileInfo.setFileSize(new File(path).length()); 273 | // fileInfo.setFilePath(path); 274 | // fileInfo.setFileName(name); 275 | // fileInfo.setTime(time); 276 | // FolderInfo folderInfo = getImageFolder(path, folderInfos); 277 | // folderInfo.getImages().add(fileInfo); 278 | // } while (cursor.moveToNext()); 279 | // } 280 | // } 281 | // cursor.close(); 282 | // } 283 | // return folderInfos; 284 | // 285 | // } 286 | // 287 | // public static List queryFilerInfo(Context context, List mlist, String selection, String[] selectionArgs) { 288 | // List fileInfos = new ArrayList<>(); 289 | // for (int i = 0; i < mlist.size(); i++) { 290 | // String[] projection = new String[]{ 291 | // MediaStore.Files.FileColumns._ID, 292 | // MediaStore.Files.FileColumns.DATA, 293 | // MediaStore.Files.FileColumns.TITLE, 294 | // MediaStore.Files.FileColumns.DATE_MODIFIED 295 | // }; 296 | // Cursor cursor = context.getContentResolver().query( 297 | // mlist.get(i), 298 | // projection, selection, 299 | // selectionArgs, projection[2] + " DESC"); 300 | // 301 | // if (cursor != null) { 302 | // if (cursor.moveToFirst()) { 303 | // int dataindex = cursor 304 | // .getColumnIndex(MediaStore.Files.FileColumns.DATA); 305 | // int nameindex = cursor 306 | // .getColumnIndex(MediaStore.Files.FileColumns.TITLE); 307 | // int timeindex = cursor 308 | // .getColumnIndex(MediaStore.Files.FileColumns.DATE_MODIFIED); 309 | // do { 310 | // FileInfo fileInfo = new FileInfo(); 311 | // String path = cursor.getString(dataindex); 312 | // String name = cursor.getString(nameindex); 313 | // String time = cursor.getString(timeindex); 314 | // fileInfo.setFileSize(new File(path).length()); 315 | // fileInfo.setFilePath(path); 316 | // fileInfo.setFileName(name); 317 | // fileInfo.setTime(time); 318 | // fileInfos.add(fileInfo); 319 | // 320 | // } while (cursor.moveToNext()); 321 | // } 322 | // } 323 | // cursor.close(); 324 | // } 325 | // return fileInfos; 326 | // 327 | // } 328 | 329 | // public static FolderInfo getImageFolder(String path, List imageFolders) { 330 | // File imageFile = new File(path); 331 | // File folderFile = imageFile.getParentFile(); 332 | // 333 | // for (FolderInfo folder : imageFolders) { 334 | // if (folder.getName().equals(folderFile.getName())) { 335 | // return folder; 336 | // } 337 | // } 338 | // FolderInfo newFolder = new FolderInfo(); 339 | // newFolder.setName(folderFile.getName()); 340 | // newFolder.setPath(folderFile.getAbsolutePath()); 341 | // imageFolders.add(newFolder); 342 | // return newFolder; 343 | // } 344 | } 345 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_folder.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 21 | 22 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |