简体   繁体   English

如何获取所有手机存储(内部存储)目录列表

[英]How to get all Phone Storage(internal storage) directory list

I want to get all files list of internal storage. 我想获取内部存储的所有文件列表。 I've write this code, 我已经写了这段代码,

File dir= getFilesDir();
        File[] list = dir.listFiles();
        for(File ff: list) {
            Toast.makeText(getApplicationContext(), ff.getName() , Toast.LENGTH_LONG).show();

        }

it just gives me Directory name: instant-run on Toast but my internal storage has many direcoties as you can see below picture, 它只是为我提供了目录名称:在Toast上即时运行 ,但是您的内部存储存在很多重复性,如下图所示, 在此处输入图片说明

kindly tell me how to get all these directories name. 请告诉我如何获取所有这些目录名称。

I found a way to list all the directory in internal storage. 我找到了一种列出内部存储中所有目录的方法。 please try this, it work's fine for me as i am using only internal storage in my phone. 请尝试此操作,因为我仅在手机中使用内部存储,所以对我来说很好。

MainActivity.java MainActivity.java

package com.sumesh.filendirectory;

import android.app.ListActivity;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

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

public class FileNDirectoryActivity extends ListActivity {

    private List<String> fileList=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File root=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        ListDir(root);
    }

    void ListDir(File f){
        File[] files=f.listFiles();
        fileList.clear();
        for (File file: files){
            fileList.add(file.getPath());
        }

        ArrayAdapter<String> directoryList=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,fileList);
        setListAdapter(directoryList);
    }
}

Manifest.xml 的Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sumesh.filendirectory">

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

    <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=".FileNDirectoryActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.xml file MainActivity.xml文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >

hope it helps. 希望能帮助到你。 if you are using android version 6 or above, once you install the app it will crash for sure because of storage run-time permission now how to resolve? 如果您使用的是android 6或更高版本,那么一旦安装了该应用程序,它肯定会由于存储运行时权限而崩溃,现在该如何解决? add run-time permission code for storage and if you are lazy follow this one. 添加用于存储的运行时权限代码,如果您很懒,请遵循此代码。

  • goto ==> installed app ==> app info screen goto ==>已安装的应用程序==>应用程序信息屏幕
  • on permission list item give storage permission then it will run smoothly. 在权限列表项上提供存储权限,然后它将平稳运行。

在此处输入图片说明

Other ways to do so: 这样做的其他方式:

MainActivity.java MainActivity.java

    public class MainActivity extends AppCompatActivity {

    ListView lview;

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

        lview = (ListView) findViewById(R.id.lview1);
        String path=Environment.getExternalStorageDirectory().getAbsolutePath();
        File f = new File(path);//converted string object to file
        String[] values = f.list();//getting the list of files in string array
        //now presenting the data into screen
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item, values);
        lview.setAdapter(adapter);//setting the adapter

    }
}

ActivityMain.xml ActivityMain.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lview1">
    </ListView>

</LinearLayout>

Manifest.xml 的Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sumesh.listview">

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

    <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>

在此处输入图片说明

Your screenshot is of what the Android SDK calls external storage , not internal storage . 您的屏幕截图是Android SDK所谓的外部存储 ,而不是内部存储 The root of external storage is obtained via Environment.getExternalStorageDirectory() . 外部存储的根是通过Environment.getExternalStorageDirectory()获得的。

You can get dirs using below code... 您可以使用以下代码获取目录...

File extStorageDir=new File(Environment.getExternalStorageDirectory());
    String fileList=extStorageDir.list();

    for(String fileName:fileList)
    sysout("FileName="+fileNAme);

尝试这个:

String dir = getFilesDir().getAbsolutePath();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取“\flash”、“\emmc”等内部存储目录 - How to get the internal storage Directory such as “\flash”、“\emmc” 如何从外部sdcard和手机存储中获取所有文件(移动存储内部)? - How to get all files from external sdcard and phone storage (internal with mobile memory)? 如何从内部存储中获取所有android手机的文件夹名称并将其显示在文本字段中? - How to get all the folder name of android phone from internal storage and show it in the text-field? 如何访问此内部存储目录? - How to access this internal storage directory? 如何从内部存储中获取不带SD卡插槽的手机中的歌曲 - How to get songs from Internal Storage for phone with no sdcard slot Android - 如何从内部存储中获取所有文件路径或从内部存储中删除所有文件 - Android - How to get all file path from internal storage or delete all files from internal storage Android中的内部手机存储 - Internal phone storage in Android 获取内部存储自定义目录位置 - Get Internal Storage custom directory location 如何将 Bitmap 图像保存到手机内部存储 - How to save Bitmap image into internal storage of Phone 获取“外部存储”的所有路径的列表时,哪个路径指向内部存储? - Which path point to the internal storage when get a list of all paths of the “external storage”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM