【问题标题】:getExternalStorageDirectory() to getExternalFilesDir()getExternalStorageDirectory() 到 getExternalFilesDir()
【发布时间】:2023-04-09 08:06:02
【问题描述】:

所以,基本上我有这个代码(所有归功于 mburhman 的文件资源管理器 - 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;
        }
    };
}

抱歉,篇幅较长。我只是想问一下为什么不能通过将文件路径更改为:

File path = getExternalFilesDir(null);

或者你如何做到这一点,以便我可以将我的文件存储到我保留的外部 SD 卡中。

编辑:

其实我在关注blog这个帖子后发现我指向的是assets文件夹。

这是指向资产文件夹的方法 https://gist.github.com/huxaiphaer/268b94a0e7959822fa679a7523701187

【问题讨论】:

  • 我想我确实知道差异。两者之间。我只需要知道如何让 getExternalFilesDir() 工作,而不是上面的工作 getExternalStorageDirectory() 代码。

标签: java android eclipse


【解决方案1】:

这基本上是可能的,但是您的应用程序的外部存储位置在不同的设备上是不同的(主要是因为某些设备将外部存储作为其集成存储的一部分)。我从 SO 的某个地方获取了下面的代码,它对我有用:

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);
    }
}

【讨论】:

  • 对不起,如果我问这个问题,但是我在哪里插入 getAbosoluteFile(rPath,this); ?
  • @NinoBelgica - 实际上在任何你想要的地方。在一些帮助类中使其成为静态的。它不使用任何外部变量:)。你可以传入任何你手头的context。您需要将path 的初始化移动到onCreatepath = getAbsolutefile("", this);
  • 我爱你。有效!我忘了提到我已经进行了 getExternalStorageState() 验证,所以我不需要在您的方法中使用 if(){}else{}。如果它没有检测到任何 sd 卡,我只想让它空白。我只需要path = this.getExternalFilesDir(null); 就不会崩溃。 :)
  • 别忘了 - 可以模拟 SD 卡。对于这种情况,您必须使用:Environment.isExternalStorageEmulated(File))
猜你喜欢
  • 2012-04-24
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 2020-10-17
  • 2014-05-27
  • 2017-10-25
相关资源
最近更新 更多