存储上不存在Android公共下载目录

时间:2015-11-09 19:35:01

标签: android android-sdcard android-file android-external-storage

我正在开发类似于文件系统资源管理器的东西,我想从下载目录开始。我使用以下代码:

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

public class LocalTabFragment extends Fragment {

    EditText addressBar;
    View view;
    String workingDirectory = null;
    Context context = getContext();
    File file;
    ArrayList<String> filenamesList;

    public void setAddressBarText(String path) {
        Log.d("path", path);
        addressBar.setText(path);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout, container, false);

        addressBar = (EditText) view.findViewById(R.id.address_bar);

        if(workingDirectory == null) {
            try {
                file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            } catch (Exception e) {
                file = context.getFilesDir();
            }

            workingDirectory = file.getPath();
        }

        Boolean fExist = file.exists();
        Log.d("local working directory", workingDirectory);
        Log.d("File exist:", fExist.toString());

        try {
            setAddressBarText(workingDirectory);
//            Log.d()
            if (file.isDirectory()) {

                File[] files = file.listFiles();

                for (File f : files) {
                    filenamesList.add(f.getName());
                }



                setupListViewAdapter(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }


    public void setupListViewAdapter(View view){
        String[] filenames = new String[filenamesList.size()];
        filenames = filenamesList.toArray(filenames);

        ListCustomAdapter adapter = new ListCustomAdapter(getActivity(), filenames);

        ListView listView = (ListView) view.findViewById(R.id.listview);
        listView.setAdapter(adapter);
    }
}

这里不会进入检查为file.isDirectory()的区块内。所以我检查file.exists();它也返回false。所以在我看来可能没有外部存储空间。如果这是真的,那么如何获得不在外部存储中且实际存在的下载目录的位置?

0 个答案:

没有答案