0

I want to show all video files names from internal storage in my application.

Environment.getExternalStorageDirectory:-I tried this one . Also First i check ExternalStorageState null or not.

My device didn't having any sdcard present.so how to deal with this? P.S i am using Moto G(2nd edition) device.

I tried:-

How to display files in listView?

How to get android device with Internal Downloader folder path

Community
  • 1
  • 1
sagar potdar
  • 598
  • 1
  • 6
  • 22
  • Search on the internet how to retrieve file names from the storage after that, search how to populate a listview with an array of strings. We help fix problems in your code, not completely write them for you – Denny May 02 '17 at 10:07
  • @Denny:- I am unable to get all the files.just got two files emulated and self. i want all the files which are present in other folder. as per your suggestion i tried this one. http://stackoverflow.com/questions/37022683/how-to-read-file-names-from-internal-storage-in-android – sagar potdar May 02 '17 at 10:23
  • It is unclear what you have in mind with internal storage. Please specify. – greenapps May 02 '17 at 10:43
  • @greenapps:- I want all video files which are stored in all directories of my device.so i just want to show that file names in my app.so if i click on one item so it will play that video this is my approach. – sagar potdar May 02 '17 at 10:50
  • please don't downvote on each question.answer the question first. i know you all are so brilliant but its not the way to help others.please try to maintain dissency. – sagar potdar May 02 '17 at 10:56
  • getExternalStorageDirectory is available on every device. So start your recursive listing there. Put all files found in an arraylist. Show your code. You probably can ask the MediaStore for all videos too. That is your choice to make. – greenapps May 02 '17 at 12:03
  • @greenapps:-Yes now i used mediaStore so i got list of all videos but Its only mp4 How I get .h264 files? – sagar potdar May 02 '17 at 12:06
  • Does the Gallery app show those files? Do they have an extension? – greenapps May 02 '17 at 12:07
  • @greenapps:No gallery didn't show these files. – sagar potdar May 02 '17 at 12:10

1 Answers1

0

This is my try where i could access internal storage files.

https://developer.android.com/training/permissions/requesting.html

From the above link where i got important information about using permission in api level>23 so there is permission issue as i am unable to access internal storage directories.

Final Code:-

public class MainActivity extends Activity {

ListView lv;
private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;

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

    lv = (ListView) findViewById(R.id.list);


    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {


        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.


        }
    } else {

        ArrayList<String> filesinfolder = GetFiles("/sdcard/");
        ArrayAdapter<String> adapter
                = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                filesinfolder);

        lv.setAdapter(adapter);


    }


}


@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                ArrayList<String> filesinfolder = GetFiles("/sdcard/");
                ArrayAdapter<String> adapter
                        = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1,
                        filesinfolder);

                lv.setAdapter(adapter);

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}


public ArrayList<String> GetFiles(String directorypath) {
    ArrayList<String> Myfiles = new ArrayList<String>();
    File f = new File(directorypath);
    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0) {
        return null;
    } else {
        for (int i = 0; i < files.length; i++)
            Myfiles.add(files[i].getName());
    }
    return Myfiles;
}

}

sagar potdar
  • 598
  • 1
  • 6
  • 22