如何访问内部存储文件夹

时间:2020-03-31 05:00:09

标签: java android directory

如何获取内部存储文件夹名称?因为我正在使用这种方法。由于我是Android的菜鸟,所以请告诉我他最简单的解决方案...

public class FirstInternalFolderActivity extends AppCompatActivity {
List<String>listFile=new ArrayList<String>();
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_internal_folder);
    ListView listview = (ListView) findViewById(R.id.listView);
File root= new File(Environment.getDataDirectory().getName());
    Toast.makeText(this,"ya ha "+root,Toast.LENGTH_SHORT).show();
ListDir(root);
}
void ListDir(File f){
    File[] files=f.listFiles();
    listFile.clear();
    for (File file:files){
        listFile.add(file.getName());
    }
    ArrayAdapter<String> dir=new ArrayAdapter<String>(this,R.layout.simple_listitem1,listFile);
  //setListAdapter(dir);

    listView.setAdapter(dir);
}
}

1 个答案:

答案 0 :(得分:1)

为了访问文件,必须在清单文件中提供权限。

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

尝试一下:

String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
    Log.d("Files", "FileName:" + files[i].getName());
}