java - getExternalStorageDirectory() 到 getExternalFilesDir()

标签 java android eclipse

所以,基本上我有这段代码(所有功劳都归功于 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 文件夹。发布。

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

最佳答案

这基本上是可能的,但您的应用程序的外部存储位置在不同的设备上是不同的(主要是因为某些设备将外部存储作为其集成存储的一部分)。我从 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);
    }
}

关于java - getExternalStorageDirectory() 到 getExternalFilesDir(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169429/

相关文章:

java - 类似于 Eclipse 中的动态面板

java - 是否可以在没有 JDK 的情况下在 Windows 上使用 eclipse?

java - 如何在 TestNG 断言中使用 OR 条件

Java 哈希码 : Object vs composite

android - 奇怪的行为 Canvas - 位图自行旋转

java.lang.NoClassDefFoundError : org/json/simple/parser/ParseException with eclipse and spring 错误

java - 使用 JenkinsRule 类构建 Jenkins 插件测试

Java执行命令但不返回值

android - Gradle 7 和 jitpack.io 在发布期间遇到错误

android - PNR 状态流程