Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujiang-gzu committed Nov 8, 2022
1 parent 9a61063 commit ada86f7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ExplorerConfig implements Serializable {
private File rootDir;
private boolean loadAsync;
private String[] allowExtensions = null;
private int explorerMode = ExplorerMode.DIRECTORY;
private int explorerMode = ExplorerMode.FILE;
private boolean showHomeDir = true;
private boolean showUpDir = true;
private boolean showHideDir = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private List<FileEntity> loadDataSync(File dir) {
* 列出指定目录下的所有子目录
*/
private List<File> listFiles(File startDir, FileFilter fileFilter) {
DialogLog.print(String.format("list dir %s", startDir));
DialogLog.print(String.format("list dir %s by filter %s", startDir, fileFilter.getClass().getName()));
if (!startDir.isDirectory()) {
return new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

package com.github.gzuliyujiang.filepicker.filter;

import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.github.gzuliyujiang.dialog.DialogLog;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
Expand All @@ -35,33 +39,51 @@ public SimpleFilter(boolean isOnlyDir, @Nullable String[] allowExtensions) {
@Override
public boolean accept(File pathname) {
if (pathname == null) {
DialogLog.print("Filter>>>pathname is null");
return false;
}
if (pathname.isDirectory()) {
DialogLog.print("Filter>>>pathname is directory: " + pathname);
return true;
}
if (isOnlyDir && pathname.isFile()) {
DialogLog.print("Filter>>>except directory but is file: " + pathname);
return false;
}
if (allowExtensions == null || allowExtensions.length == 0) {
DialogLog.print("Filter>>>allow extensions is empty: " + pathname);
return true;
}
//返回当前目录所有以某些扩展名结尾的文件
String extension = getExtension(pathname.getPath());
return Arrays.toString(allowExtensions).contains(extension);
DialogLog.print("Filter>>>extension of " + pathname + ": " + extension);
boolean contains = false;
for (String allowExtension : allowExtensions) {
if (TextUtils.isEmpty(allowExtension) || allowExtension.contains(extension)) {
contains = true;
break;
}
}
DialogLog.print("Filter>>>allow extensions is " + Arrays.toString(allowExtensions) + ", contains: " + contains);
return contains;
}

private String getExtension(String path) {
if (path == null) {
return "";
}
String ext = "";
int slashPos = path.lastIndexOf(File.separator);
if (slashPos != -1) {
path = path.substring(slashPos);
}
int dotPos = path.lastIndexOf('.');
if (0 <= dotPos) {
return path.substring(dotPos + 1);
if (dotPos != -1) {
ext = path.substring(dotPos + 1);
} else {
return "";
ext = path;
}
return ext;
}

}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.github.gzuliyujiang.fallback">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

<application
android:name=".DemoApp"
android:allowBackup="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FileExplorer fileExplorer = view.findViewById(R.id.fileExplorer);
ExplorerConfig config = new ExplorerConfig(requireContext());
config.setRootDir(new File("sdcard"));
config.setLoadAsync(true);
config.setExplorerMode(ExplorerMode.FILE);
config.setShowHomeDir(false);
config.setShowUpDir(false);
config.setShowHideDir(false);
FileExplorer fileExplorer = view.findViewById(R.id.fileExplorer);
//config.setAllowExtensions(new String[]{".txt", ".jpg"});
fileExplorer.load(config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

package com.github.gzuliyujiang.fallback.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

Expand Down Expand Up @@ -42,14 +46,15 @@ public class FilePickerActivity extends BackAbleActivity implements OnFilePicked
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker_file);
FileExplorer fileExplorer = findViewById(R.id.file_picker_explorer);
ExplorerConfig config = new ExplorerConfig(this);
config.setRootDir(Environment.getExternalStorageDirectory());
config.setLoadAsync(true);
config.setExplorerMode(ExplorerMode.FILE);
config.setShowHomeDir(true);
config.setShowUpDir(true);
config.setShowHideDir(true);
FileExplorer fileExplorer = findViewById(R.id.file_picker_explorer);
config.setAllowExtensions(new String[]{".txt", ".jpg"});
fileExplorer.load(config);
}

Expand All @@ -58,6 +63,19 @@ public void onFilePicked(@NonNull File file) {
Toast.makeText(getApplicationContext(), file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
}

public void onPermission(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
Toast.makeText(getApplicationContext(), "isExternalStorageManager==true", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
startActivity(intent);
}
}
Toast.makeText(getApplicationContext(), "当前系统版本不支持", Toast.LENGTH_SHORT).show();
}

public void onFilePick(View view) {
ExplorerConfig config = new ExplorerConfig(this);
config.setRootDir(getExternalFilesDir(null));
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_picker_file.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@

<include layout="@layout/layout_title_bar" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onPermission"
android:text="所有文件访问权限" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit ada86f7

Please sign in to comment.