8

So, basically I have this code (all credits to mburhman's File Explorer - https://github.com/mburman/Android-File-Explore):

private File path = new File(Environment.getExternalStorageDirectory() + "");

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start);

    loadFileList();

    showDialog(DIALOG_LOAD_FILE);

    Log.d(TAG, path.getAbsolutePath());

    readDir = (Button) findViewById(R.id.btnReadDirectory);

    readDir.setOnClickListener(this);
}
private void loadFileList() {
    try {
        path.mkdirs();
    } catch (SecurityException e) {
        Log.e(TAG, "unable to write on the sd card ");
    }

    if (path.exists()) {
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                // TODO Auto-generated method stub
                File sel = new File(dir, filename);
                // Filters based on whether the file is hidden or not
                return (sel.isFile() || sel.isDirectory())
                        && !sel.isHidden();
            }
        };

        String[] fList = path.list(filter);
        fileList = new Item[fList.length];
        for (int i = 0; i < fList.length; i++) {
            fileList[i] = new Item(fList[i], R.drawable.file_icon);

            File sel = new File(path, fList[i]);

            if (sel.isDirectory()) {
                fileList[i].icon = R.drawable.directory_icon;
                Log.d("DIRECTORY", fileList[i].file);
            } else {
                Log.d("FILE", fileList[i].file);
            }
        }

        if (!firstLvl) {
            Item temp[] = new Item[fileList.length + 1];
            for (int i = 0; i < fileList.length; i++) {
                temp[i + 1] = fileList[i];
            }
            temp[0] = new Item("Up", R.drawable.directory_up);
            fileList = temp;
        }
    } else {
        Log.e(TAG, "path does not exist");
        UIHelper.displayText(this, R.id.tvPath, "Path does not exist");
    }

    adapter = new ArrayAdapter<Item>(this,
            android.R.layout.select_dialog_item, android.R.id.text1,
            fileList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view
                    .findViewById(android.R.id.text1);

            textView.setCompoundDrawablesWithIntrinsicBounds(
                    fileList[position].icon, 0, 0, 0);
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            textView.setCompoundDrawablePadding(dp5);

            return view;
        }
    };
}

Sorry for it being long. I just want to ask why is it not possible by changing File path to:

File path = getExternalFilesDir(null);

or how do you make it happen so I can store my files to my reserved external SD Card.

EDIT:

Actually, found out that I was pointing to the assets folder since I was following this blog post.

This is method which points to the assets folder https://gist.github.com/huxaiphaer/268b94a0e7959822fa679a7523701187

Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77
Nino Belgica
  • 83
  • 1
  • 1
  • 4
  • Have a look at this http://stackoverflow.com/questions/10123812/diff-between-getexternalfilesdir-and-getexternalstoragedirectory – Mohit Aug 11 '13 at 06:45
  • I think I do know the diff. between the two. I just need to know how to make getExternalFilesDir() to work instead of the working getExternalStorageDirectory() code above. – Nino Belgica Aug 11 '13 at 07:29

1 Answers1

11

It basically is possible, but the place of the external storage for your application is different on different devices (basically because some devices have the external as part of their integrated storage). I have taken the code below from somewhere on SO and it works for me:

private File getAbsoluteFile(String relativePath, Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        return new File(context.getExternalFilesDir(null), relativePath);
    } else {
        return new File(context.getFilesDir(), relativePath);
    }
}
Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Sorry, if I asked this noobish, but where do I insert getAbosoluteFile(rPath,this); ? – Nino Belgica Aug 11 '13 at 07:31
  • @NinoBelgica - actually wherever you want. Make it static in some helper class. It does not use any external variables :). You can pass in whatever `context` you have at hand. You will need to move the initialization of `path` in the `onCreate`: `path = getAbsolutefile("", this);` – Boris Strandjev Aug 11 '13 at 07:44
  • 1
    I love you man. It worked! I forgot to mention I had the getExternalStorageState() validation already so I didnt need to use if(){}else{} in your method. I just want it blank if it dont detect any sd card. I just need `path = this.getExternalFilesDir(null);` for it not to crash. :) – Nino Belgica Aug 11 '13 at 08:18
  • Dont forget - SD card can be emulated. For that cases you must use: `Environment.isExternalStorageEmulated(File))` – Bokili Production Apr 22 '21 at 01:31