Erstellen Sie einen einfachen File Dialog Finder in Android

View more Tutorials:

1- Das Zweck vom Beispiel

In diesem Artikel werde ich Sie anleiten, eine einfache Anwendung zu erstellen, die Dateien im  Android zu suchen und die Ergebnisse in ein  Dialog ​​​​​​​anzuzeigen.
Diese Anwendung wird die Dateien in  SD Card suchen. Wenn Sie daher diese Anwendung in  Android Emulator prüfen, sollen Sie  SD Card für ihn einstellen.
Verwenden Sie  Device File Explorer um einige Dateien zum  Android Emulator zu kopieren. Es ist notwendig damit Sie die Anwendung prüfen können.

2- Zum Beispiel: File Finder Dialog

Im  Android Studio erstellen Sie ein neues Projekt:
  • File > New > New Project > Empty Activity
    • Name: FileFinderDialogExample
    • Package name: org.o7planning.filefinderdialogexample
    • Language: Java
Nachdem der Benutzer auf die Suchen-Button geklickt hat, wird eine Liste der gefundenen Dateien in einer  RecyclerView angezeigt. Da es eine Interface-Komponente ist, die im  Android SDK nicht verfügbar ist, müssen Sie sie im Projekt installieren.
Nachdem die Bibliothek erfolgreich installiert wurde, wird sie in  build.gradle (Module App) deklariert:
dependencies {
    ...
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
Zunächst registrieren Sie sich beim System, damit Ihre Anwendung auf die Dateien zugreifen kann.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.o7planning.filefinderdialogexample">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Die Interface der Anwendung:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText_search"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:ems="10"
        android:hint="File Name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox_isRegex"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="Regex?"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_search" />

    <Button
        android:id="@+id/button_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Search"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox_isRegex" />
</androidx.constraintlayout.widget.ConstraintLayout>
Die Klasse  FileFinder enthält die Methoden zum Suchen von Dateien im System. Beachten Sie, dass bei  Android 6.0+ (API Level 23+) Ihre Anwendung den Benutzer um Erlaubnis bitten muss, um Zugriff auf die Dateien zu erhalten. Wenn dies vom Benutzer nicht zugelassen wird, geben diese Methode immer einer leere Liste zurück. 
FileFinder.java
package org.o7planning.filefinderdialogexample;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class FileFinder {

    private static final String LOG_TAG_FILE_FINDER = "FileSelector";

    private boolean enableLog;

    public FileFinder(boolean enableLog) {
        this.enableLog = enableLog;
    }

    public List<File> findByKeyword(File rootDir, String keywordFileName) {
        if(keywordFileName==null || keywordFileName.isEmpty())  {
            return new ArrayList<File>();
        }
        String regexFileName = keywordFileName.replace("*", ".*?");

        List<File> resultList = this.listOfFile(rootDir, regexFileName);
        return resultList;
    }

    public List<File> findByRegex(File rootDir, String regexFileName) {

        List<File> resultList = this.listOfFile(rootDir, regexFileName);
        return resultList;
    }

    public List<File> findInSDCardByKeyword(String keywordFileName)  {
        // (Example): /storage/emulated/0
        String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        this.log("External Storage Directory: " + sdCardPath);

        File sdCardDir = new File(sdCardPath);
        return this.findByKeyword(sdCardDir, keywordFileName);
    }

    public List<File> findInSDCardByRegex(String regexFileName)  {
        // (Example): /storage/emulated/0
        String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        this.log("External Storage Directory: " + sdCardPath);

        File sdCardDir = new File(sdCardPath);
        return this.findByRegex(sdCardDir, regexFileName);
    }

    private List<File> listOfFile(File dir, String regexFileName) {
        Pattern patternFileName = Pattern.compile(regexFileName);

        List<File> resultList = new ArrayList<File>();
        this.listOfFile(dir, patternFileName, resultList);
        return resultList;
    }

    private void listOfFile(File dir, Pattern patternFileName, List<File> resultList) {
        this.log("LIST OF DIR " + dir.getAbsolutePath());
        File[] list = dir.listFiles();

        if(list == null)  {
            this.log("Directory" + dir.getAbsolutePath()+ " has no files");
            return;
        }
        this.log("Directory" + dir.getAbsolutePath()+ " has " + list.length +" direct files");

        for (File file : list) {
            if (file.isDirectory()) {
                if (!new File(file, ".nomedia").exists() && !file.getName().startsWith(".")) {
                    this.log( "IS DIR " + file);
                    listOfFile(file, patternFileName, resultList);
                }
            } else {
                String path = file.getAbsolutePath();
                this.log( "FILE PATH: " + path);

                String fileName = file.getName();
                if(patternFileName.matcher(fileName).find())  {
                    resultList.add(file);
                    this.log( "ADD " + path);
                }
            }
        }
    }

    private void log(String message)  {
        if(enableLog) {
            Log.i(LOG_TAG_FILE_FINDER, message);
        }
    }
}
OnFileSelectListener.java
package org.o7planning.filefinderdialogexample;

import java.io.File;

public interface OnFileSelectListener {
    void onSelect(File file);
}
ResultDialog.java
package org.o7planning.filefinderdialogexample;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.List;

public class ResultDialog extends Dialog {

    public ResultDialog(@NonNull Context context, List<File> resultList, OnFileSelectListener listener) {
        super(context);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //
        LinearLayout linearLayout = new LinearLayout(this.getContext());
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        int p = convertToPixels(12);
        linearLayout.setPadding(p, p, p, p);
        linearLayout.setGravity(Gravity.CENTER);

        TextView textView = new TextView(this.getContext());
        textView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth(), ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("~ Search Result ~");

        RecyclerView recyclerView = new RecyclerView(this.getContext());

        linearLayout.addView(textView);
        linearLayout.addView(recyclerView);

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(this.getContext(), this, listener, resultList);
        recyclerView.setAdapter(adapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //
        // this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        //
        this.setContentView(linearLayout);
        this.setCancelable(true);
    }

    private int convertToPixels(int dp) {
        float scale = this.getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    private int screenWidth() {
        return this.getContext().getResources().getDisplayMetrics().widthPixels;
    }

    // RecyclerViewAdapter
    private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

        private Context context;

        private List<File> files;
        private OnFileSelectListener listener;
        private Dialog dialog;

        public RecyclerViewAdapter(Context context, Dialog dialog, OnFileSelectListener listener, List<File> files) {
            this.context = context;
            this.files = files;
            this.listener = listener;
            this.dialog = dialog;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            TextView txtName = new TextView(context);
            TextView txtPath = new TextView(context);
            txtPath.setTypeface(txtPath.getTypeface(), Typeface.ITALIC);
            txtPath.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);

            linearLayout.addView(txtName);
            linearLayout.addView(txtPath);

            RecyclerViewAdapter.ViewHolder viewHolder = new RecyclerViewAdapter.ViewHolder(linearLayout);
            return viewHolder;
        }

        // Inner class to hold a reference to each item of RecyclerView
        public class ViewHolder extends RecyclerView.ViewHolder {

            public LinearLayout linearLayout;
            public TextView textViewFileName;
            public TextView textViewPath;

            public ViewHolder(View itemLayoutView) {
                super(itemLayoutView);

                this.linearLayout = (LinearLayout) itemLayoutView;
                this.textViewFileName = (TextView) linearLayout.getChildAt(0);
                this.textViewPath = (TextView) linearLayout.getChildAt(1);
            }
        }


        @Override
        public int getItemCount() {
            return files.size();
        }

        @Override
        public void onBindViewHolder(RecyclerViewAdapter.ViewHolder viewHolder, final int position) {
            final File selectedFile = files.get(position);

            final String path = selectedFile.getAbsolutePath();
            String[] split = path.split("/");
            final String name = split[split.length - 1];

            viewHolder.textViewFileName.setText(name);
            viewHolder.textViewPath.setText(path);

            viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    listener.onSelect(selectedFile);
                }
            });
        }
    }
}
MainActivity.java
package org.o7planning.filefinderdialogexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int MY_REQUEST_CODE_PERMISSION = 1000;

    private static final String LOG_TAG = "AndroidExample";

    private Button buttonSearch;

    private EditText editTextSearch;
    private CheckBox checkBoxIsRegex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.editTextSearch = (EditText) this.findViewById(R.id.editText_search);
        this.checkBoxIsRegex = (CheckBox) this.findViewById(R.id.checkBox_isRegex);

        this.buttonSearch = (Button) this.findViewById(R.id.button_search);

        this.buttonSearch.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                askPermissionAndSearchFile();
            }
        });
    }

    private void askPermissionAndSearchFile()  {
        // With Android Level >= 23, you have to ask the user
        // for permission to access External Storage.
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { // Level 23

            // Check if we have Call permission
            int permisson = ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE);

            if (permisson != PackageManager.PERMISSION_GRANTED) {
                // If don't have permission so prompt the user.
                this.requestPermissions(
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_REQUEST_CODE_PERMISSION
                );
                return;
            }
        }
        this.doSearchFile();
    }

    private void doSearchFile()   {
        FileFinder fileSelector = new FileFinder(true);
        String searchText = this.editTextSearch.getText().toString();
        boolean isRegex = this.checkBoxIsRegex.isChecked();

        List<File> resultList = null;
        if(isRegex)  {
            resultList = fileSelector.findInSDCardByRegex(searchText);
        } else {
            resultList = fileSelector.findInSDCardByKeyword(searchText);
        }

        ResultDialog dialog = new ResultDialog(this,  resultList,  new OnFileSelectListener() {
            @Override
            public void onSelect(File file) {
                Toast.makeText(MainActivity.this, "Path: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
            }
        });
        dialog.show();
    }

    // When you have the request results
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //
        switch (requestCode) {
            case MY_REQUEST_CODE_PERMISSION: {

                // Note: If request is cancelled, the result arrays are empty.
                // Permissions granted (CALL_PHONE).
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Log.i( LOG_TAG,"Permission granted!");
                    Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();

                    this.doSearchFile();
                }
                // Cancelled or denied.
                else {
                    Log.i(LOG_TAG,"Permission denied!");
                    Toast.makeText(this, "Permission denied!", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }


}

View more Tutorials: