0

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, enter image description here

kindly tell me how to get all these directories name.

Zohaib
  • 189
  • 1
  • 5
  • 16
  • they cannot resolve my issue. I want to get list of internal storage not external storage. – Zohaib Jan 24 '17 at 17:01
  • `/Home/Phone storage/` is not a valid path for an Android device. Better use a decent file explorer app. This one should be avoided and its programmer(s) fired. – greenapps Jan 24 '17 at 17:03
  • then how i can distinguish internal storage and external storage (SD card) in our app – Zohaib Jan 24 '17 at 17:09
  • 1
    Your question is wrong. And new as there is internal storage AND external storage AND sd card. Three. Please stay with your original problem. – greenapps Jan 24 '17 at 17:11

4 Answers4

7

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

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

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

    <?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? add run-time permission code for storage and if you are lazy follow this one.

  • goto ==> installed app ==> app info screen
  • on permission list item give storage permission then it will run smoothly.

enter image description here

Other ways to do so:

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

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

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

enter image description here

curious
  • 176
  • 2
  • 7
0

Your screenshot is of what the Android SDK calls external storage, not internal storage. The root of external storage is obtained via Environment.getExternalStorageDirectory().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i know about getExternalStorageDirectory() but i want to get internal storage directories. How can i get? – Zohaib Jan 24 '17 at 16:51
  • @Zohaib: Let me try boldface: **your screenshot is of what the Android SDK calls external storage, not internal storage**. If you want to reproduce what is in the screenshot, use `Environment.getExternalStorageDirectory()`. You cannot "get all these directories name" using `getFilesDir()`, because "all these directories" are on external storage. – CommonsWare Jan 24 '17 at 17:14
  • @CommonsWare as of API 29 Environment.getExternalStorageDirectory() is deprecated. Is there any other approach? – mad_lad May 04 '20 at 15:46
  • @MADLAD: On Android 10, once your `targetSdkVersion` reaches 29 or higher, you will not be able to read content on external storage, except in a few limited places. On Android 11, you get that access back, but this method remains deprecated. Instead, you can use `StorageManager` to find a `StorageVolume`, then call [`getDirectory()`](https://developer.android.com/reference/android/os/storage/StorageVolume?#getDirectory()) on that. AFAIK, that's the long-term replacement for `Environment.getExternalStorageDirectory()`. – CommonsWare May 04 '20 at 16:16
0

You can get dirs using below code...

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

    for(String fileName:fileList)
    sysout("FileName="+fileNAme);
halfer
  • 19,824
  • 17
  • 99
  • 186
Hiren Jungi
  • 854
  • 8
  • 20
0

Try this:

String dir = getFilesDir().getAbsolutePath();
cru3lgenius
  • 106
  • 1
  • 7