A Safe Path to External Storage

前端 未结 5 1644
暗喜
暗喜 2020-12-22 11:14

For my app, I have a fairly large database that needs to be stored on the user\'s device. I plan to change this in the future. For now, however, I\'d like to store it to the

5条回答
  •  别那么骄傲
    2020-12-22 12:06

    We may want to consider getting the parent directory instead...

    String myPath = Environment.getExternalStorageDirectory().getParent();
    //mypath = /storage
    

    In my case, I have a GS4 and a Note 10.1. The above returned a folder called "storage" which contains the sdCard0 (internal) and extSdCard (memory card) folders.

    I'm playing around with a file explorer type app, but I'm only interested in showing the mounts. This displays extCard and sdcard0 in the ListView after filtering out the system folders with the .isHidden() and .canRead() methods.

    public class MainActivity extends Activity {
    
    private ListView lvFiles;
    private TextView label;
    private List item = null;
    private List path = null;
    private String intRoot, extRoot;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        lvFiles = (ListView) findViewById(R.id.lvFiles);
        label = (TextView) findViewById(R.id.tvLabel);
    
    
    
        extRoot = Environment.getExternalStorageDirectory().getParent();
    
        label.setText(extRoot);
        displayFiles(extRoot);
    
    }
    
    /** Inflate menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater m = getMenuInflater();
        m.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    private void displayFiles(String p) {
    
        File f = new File(p);
        File[] files = f.listFiles();
    
        item = new ArrayList();
        path = new ArrayList();
    
        for (int i=0; i filelist = new ArrayAdapter(this, R.layout.row, item);
        lvFiles.setAdapter(filelist);
    
    }
    

    Of course, I haven't tested this with none-Samsung devices. Can't wait for KitKat, as it will include methods for developers to easily find the memory card.

提交回复
热议问题