Listing internal memory files in Listview

Asked

Viewed 269 times

2

I intend to create a app that by clicking on "Lists" create a list using ListView based on files found in a particular internal memory folder(path).

  • 1

    Hello Ekson, welcome to Sopt. To be more likely to get an answer that solves your problem, try to clarify the question better, describing what difficulties you encountered in creating this list, and other details that might be important to understand what you need. Taking a look at this thread http://answall.com/questions/how-to-ask-beta from Help Center can also help you format your question, further increasing the chances of someone with technical knowledge (and has plenty here) helping you.

1 answer

1

Thinking a little bit it is possible to make several internal and external device listings in an application using the Environment and Context.

First, as an example, it is necessary to grant permission to read your directories on manifest.xml if you want to make a listing of external files using READ_EXTERNAL_STORAGE. If by any chance you use the getRootDirectory() this permission is not required. And if you are using Android API 6.0+, you will have to read a little more about Requesting Permissions at Run Time.

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

In his main.xml, will insert your ListView to list all files:

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

And finally in your class Main It’s gonna do this way:

    ListView listView ;
    ArrayList<String> list = new ArrayList<String>();
    listView = (ListView) findViewById(R.id.list);

    String path = Environment.getRootDirectory().toString();
    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++)
    {
        list.add(files[i].getName());
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, list);
    // Assign adapter to ListView
    listView.setAdapter(adapter);

If you want to list other directories in a specific folder, just put the name of the folder in your path:

String path = Environment.getRootDirectory().toString()+"/"+diretorioEspecifico;
  • The AP speaks in "List files from memory internal in Listview".

  • 1

    @ramaral I improved the response, including internal memory files. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.