[英]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.
添加用于存储的运行时权限代码,如果您很懒,请遵循此代码。
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.