GetExternalStorageDirectory () for getExternalFilesDir ()

So basically I have this code (all mberhman File Explorer credits - 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; } }; } 

I apologize for this. I just want to ask why this is not possible by changing the file path:

 File path = getExternalFilesDir(null); 

or how to do it so that I can store my files on my backup external SD card.

+6
source share
1 answer

In principle, this is possible, but the location of the external storage for your application differs on different devices (mainly because some devices have an external component as part of the built-in storage). I took the code below 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); } } 
+5
source

Source: https://habr.com/ru/post/951478/


All Articles