android:获取隐藏目录

时间:2017-08-05 06:05:33

标签: android file directory

我试图从隐藏目录中获取所有文件。但它没有显示任何文件。 请帮我解决这个问题。

public class Images extends Fragment {
    private static final String WHATSAPP_STATUSES_LOCATION_L = Environment.getExternalStorageDirectory().toString() + "/WhatsApp/Media/.Statuses/";
    private RecyclerView mRecyclerViewMediaList;
    private LinearLayoutManager mLinearLayoutManager;

    public Images() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.images, container, false);
        mRecyclerViewMediaList = (RecyclerView) v.findViewById(R.id.recyclerViewMedia);
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerViewMediaList.setLayoutManager(mLinearLayoutManager);
        RecyclerViewMediaAdapter recyclerViewMediaAdapter = new RecyclerViewMediaAdapter(this.getListFiles(new File(WHATSAPP_STATUSES_LOCATION_L)), getActivity());
        mRecyclerViewMediaList.setAdapter(recyclerViewMediaAdapter);
        return v;
    }

    /**
     * get all the files in specified directory
     *
     * @param parentDir
     * @return
     */
    private ArrayList<File> getListFiles(File parentDir) {
        Log.e("getpath: ", String.valueOf(parentDir));
        ArrayList<File> inFiles = new ArrayList<File>();
        File[] files;
        files = parentDir.listFiles();
        Log.e("Files: ", String.valueOf(files));
        if (files != null) {
            for (File file : files) {

                if (file.getName().endsWith(".jpg")
                    // || file.getName().endsWith(".gif") ||
                    //   file.getName().endsWith(".mp4")
                        ) {
                    if (!inFiles.contains(file))
                        inFiles.add(file);
                }
            }
            Log.e("getListFiles: ", String.valueOf(inFiles));
        }
        return inFiles;
    }

}

Logcat显示

08-05 11:17:12.517 32636-32636/? I/art: Late-enabling -Xcheck:jni
08-05 11:17:12.583 32636-32636/rubin.whatsstatus D/LenovoAppIconTheme: ExtraResources;cleanCachedIcon;clear cache..
08-05 11:17:12.804 32636-32636/rubin.whatsstatus W/System: ClassLoader referenced unknown path: /data/app/rubin.whatsstatus-2/lib/arm
08-05 11:17:12.819 32636-32636/rubin.whatsstatus I/InstantRun: starting instant run server: is main process
08-05 11:17:12.903 32636-32636/rubin.whatsstatus W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-05 11:17:12.955 32636-32636/rubin.whatsstatus V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
08-05 11:17:12.955 32636-32636/rubin.whatsstatus V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
08-05 11:17:12.956 32636-32636/rubin.whatsstatus V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
08-05 11:17:12.956 32636-32636/rubin.whatsstatus V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
08-05 11:17:12.956 32636-32636/rubin.whatsstatus V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
08-05 11:17:12.958 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9f26236
08-05 11:17:12.958 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@1e47537
08-05 11:17:13.123 32636-32668/rubin.whatsstatus I/Adreno: QUALCOMM build                   : 5817cca, Ie9c95840c4
                                                           Build Date                       : 03/26/17
                                                           OpenGL ES Shader Compiler Version: XE031.09.00.03
                                                           Local Branch                     : 
                                                           Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.5.5.R1.07.00.00.269.021
                                                           Remote Branch                    : NONE
                                                           Reconstruct Branch               : NOTHING
08-05 11:17:13.132 32636-32668/rubin.whatsstatus I/OpenGLRenderer: Initialized EGL, version 1.4
08-05 11:17:13.132 32636-32668/rubin.whatsstatus D/OpenGLRenderer: Swap behavior 1
08-05 11:17:13.161 32636-32636/rubin.whatsstatus W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
08-05 11:17:15.208 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@43a28a5
08-05 11:17:15.208 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9c3c47a
08-05 11:17:15.235 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@28d1088
08-05 11:17:15.382 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@2e9abb8
08-05 11:17:15.382 32636-32636/rubin.whatsstatus V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@8205d91
08-05 11:17:15.412 32636-32636/rubin.whatsstatus E/getpath:: /storage/emulated/0/WhatsApp/Media/.Statuses
08-05 11:17:15.415 32636-32636/rubin.whatsstatus E/Files:: null

并且目录不为空,我通过文件管理器检查了它 enter image description here

文件管理器的路径是 enter image description here

1 个答案:

答案 0 :(得分:1)

我建议您检查权限如果路径正确

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />