无法读取文件夹。 (Android-文件-对话框)

Posted

技术标签:

【中文标题】无法读取文件夹。 (Android-文件-对话框)【英文标题】:Cant read a folder. (Android-File-Dialog) 【发布时间】:2015-05-20 09:23:48 【问题描述】:

在我的应用程序中,我需要读写文件 (xml)。我编写了一个自定义对话框,并将文件保存在 getExternalFilesDir 或 getExternalStoragePublicDirectory 或内部存储器中,如 android 开发人员指南所述。正在写入文件,没有任何问题。但是当我尝试读取这些 xml 文件(它们存储在 /storage/0/emulated 或 /data/data/myAPP/files 中)时,使用 android-file-dialog 它表示无法读取文件夹存储和数据。 sys或者system等其他的可以正常读取,而我授权的可以读写。

我正在使用 android 4.4.4(root Kitkat)。我的应用程序具有所有需要的权限。这是我正在使用的代码。

public class SaveViewDialog extends DialogFragment 

private LayoutInflater inflater;
private View v;
private EditText name;
private List<RowData> lista;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) 
    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.filenamedialog_layout,null);
    name=(EditText)v.findViewById(R.id.nombre);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v).setPositiveButton("Guardar", new DialogInterface.OnClickListener() 
        @Override
        public void onClick(DialogInterface dialog, int which) 
            if (!name.getText().toString().isEmpty()) 
                String contenido = createContent();
                boolean flag = true;
                try 
                    File file = new File(getActivity().getExternalFilesDir(null), name.getText().toString() + ".vista.xml");
                    //File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), name.getText().toString() + ".vista.xml");
                    //File file = new File("/extSdCard/",name.getText().toString() + ".vista.xml");
                    /*OutputStreamWriter fout = new OutputStreamWriter(
                        getActivity().openFileOutput(file.getCanonicalPath(), Context.MODE_WORLD_READABLE));*/
                    Log.e("Sitio del archivo",file.getAbsolutePath());
                    Toast.makeText(getActivity(),"File saved at: "+file.getAbsolutePath(),Toast.LENGTH_SHORT).show();
                    FileOutputStream fout = new FileOutputStream(file);
                    fout.write(contenido.getBytes());
                    fout.close();
                 catch (Exception ex) 
                    ex.printStackTrace();
                    Log.e("Ficheros", "Error al escribir fichero a memoria interna");
                    flag = false;
                    Toast.makeText(getActivity(), "Hubo un error al guardar el archivo", Toast.LENGTH_LONG).show();
                
                if (flag) 
                    Toast.makeText(getActivity(), "Archivo guardado con exito", Toast.LENGTH_LONG).show();
                
             else 
                Toast.makeText(getActivity(), "Introduce un nombre para el archivo", Toast.LENGTH_LONG).show();
            
        
    ).setNegativeButton("Cancelar", new DialogInterface.OnClickListener() 
        @Override
        public void onClick(DialogInterface dialog, int which) 
        
    );
    return builder.create();

@Override
public void setArguments(Bundle args) 
    lista = new ArrayList<RowData>();
    int tam = args.getInt("tam");
    for(int i=0;i<tam;i++)
        String tipo = args.getString("tipo"+i);
        String variables = args.getString("variables"+i);
        String ubicacion = args.getString("ubicacion"+i);
        lista.add(new RowData(tipo,variables,ubicacion));
    


private String createContent()
    String contenido = "<lista>\n";
    for (int i = 0; i < lista.size(); i++) 
        contenido += "\t<elemento>\n";
        contenido += "\t\t<tipo>" + lista.get(i).getTipo() + "</tipo>\n";
        contenido += "\t\t<variables>" + lista.get(i).getVariables() + "</variables>\n";
        contenido += "\t\t<ubicacion>" + lista.get(i).getUbicacion() + "</ubicacion>\n";
        contenido += "\t</elemento>\n";
    
    contenido += "</lista>\n";
    return contenido;

为了阅读,我使用的是链接中的那个:

public class FileDialog extends ListActivity 

/**
 * Chave de um item da lista de paths.
 */
private static final String ITEM_KEY = "key";

/**
 * Imagem de um item da lista de paths (diretorio ou arquivo).
 */
private static final String ITEM_IMAGE = "image";

/**
 * Diretorio raiz.
 */
private static final String ROOT = "/";

/**
 * Parametro de entrada da Activity: path inicial. Padrao: ROOT.
 */
public static final String START_PATH = "START_PATH";

/**
 * Parametro de entrada da Activity: filtro de formatos de arquivos. Padrao:
 * null.
 */
public static final String FORMAT_FILTER = "FORMAT_FILTER";

/**
 * Parametro de saida da Activity: path escolhido. Padrao: null.
 */
public static final String RESULT_PATH = "RESULT_PATH";

/**
 * Parametro de entrada da Activity: tipo de selecao: pode criar novos paths
 * ou nao. Padrao: nao permite.
 *
 * @see @link SelectionMode
 */
public static final String SELECTION_MODE = "SELECTION_MODE";

/**
 * Parametro de entrada da Activity: se e permitido escolher diretorios.
 * Padrao: falso.
 */
public static final String CAN_SELECT_DIR = "CAN_SELECT_DIR";

private List<String> path = null;
private TextView myPath;
private EditText mFileName;
private ArrayList<HashMap<String, Object>> mList;

private Button selectButton;

private LinearLayout layoutSelect;
private LinearLayout layoutCreate;
private InputMethodManager inputManager;
private String parentPath;
private String currentPath = ROOT;

private int selectionMode = SelectionMode.MODE_CREATE;

private String[] formatFilter = null;

private boolean canSelectDir = false;

private File selectedFile;
private HashMap<String, Integer> lastPositions = new HashMap<String, Integer>();

/**
 * Called when the activity is first created. Configura todos os parametros
 * de entrada e das VIEWS..
 */
@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setResult(RESULT_CANCELED, getIntent());

    setContentView(R.layout.file_dialog_main);
    myPath = (TextView) findViewById(R.id.path);
    mFileName = (EditText) findViewById(R.id.fdEditTextFile);

    inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

    selectButton = (Button) findViewById(R.id.fdButtonSelect);
    selectButton.setEnabled(false);
    selectButton.setOnClickListener(new OnClickListener() 
        @Override
        public void onClick(View v) 
            if (selectedFile != null) 
                getIntent().putExtra(RESULT_PATH, selectedFile.getPath());
                setResult(RESULT_OK, getIntent());
                finish();
            
        
    );

    final Button newButton = (Button) findViewById(R.id.fdButtonNew);
    newButton.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            setCreateVisible(v);

            mFileName.setText("");
            mFileName.requestFocus();
        
    );

    selectionMode = getIntent().getIntExtra(SELECTION_MODE, SelectionMode.MODE_CREATE);
    formatFilter = getIntent().getStringArrayExtra(FORMAT_FILTER);
    canSelectDir = getIntent().getBooleanExtra(CAN_SELECT_DIR, false);

    if (selectionMode == SelectionMode.MODE_OPEN) 
        newButton.setEnabled(false);
    

    layoutSelect = (LinearLayout) findViewById(R.id.fdLinearLayoutSelect);
    layoutCreate = (LinearLayout) findViewById(R.id.fdLinearLayoutCreate);
    layoutCreate.setVisibility(View.GONE);

    final Button cancelButton = (Button) findViewById(R.id.fdButtonCancel);
    cancelButton.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            setSelectVisible(v);
        

    );
    final Button createButton = (Button) findViewById(R.id.fdButtonCreate);
    createButton.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            if (mFileName.getText().length() > 0) 
                getIntent().putExtra(RESULT_PATH, currentPath + "/" + mFileName.getText());
                setResult(RESULT_OK, getIntent());
                finish();
            
        
    );

    String startPath = getIntent().getStringExtra(START_PATH);
    startPath = startPath != null ? startPath : ROOT;
    if (canSelectDir) 
        File file = new File(startPath);
        selectedFile = file;
        selectButton.setEnabled(true);
    
    getDir(startPath);


private void getDir(String dirPath) 

    boolean useAutoSelection = dirPath.length() < currentPath.length();

    Integer position = lastPositions.get(parentPath);

    getDirImpl(dirPath);

    if (position != null && useAutoSelection) 
        getListView().setSelection(position);
    



/**
 * Monta a estrutura de arquivos e diretorios filhos do diretorio fornecido.
 *
 * @param dirPath
 *            Diretorio pai.
 */
private void getDirImpl(final String dirPath) 

    currentPath = dirPath;

    final List<String> item = new ArrayList<String>();
    path = new ArrayList<String>();
    mList = new ArrayList<HashMap<String, Object>>();

    File f = new File(currentPath);
    File[] files = f.listFiles();
    if (files == null) 
        currentPath = ROOT;
        f = new File(currentPath);
        files = f.listFiles();
    
    myPath.setText(getText(R.string.location) + ": " + currentPath);

    if (!currentPath.equals(ROOT)) 

        item.add(ROOT);
        addItem(ROOT, R.drawable.folder);
        path.add(ROOT);

        item.add("../");
        addItem("../", R.drawable.folder);
        path.add(f.getParent());
        parentPath = f.getParent();

    

    TreeMap<String, String> dirsMap = new TreeMap<String, String>();
    TreeMap<String, String> dirsPathMap = new TreeMap<String, String>();
    TreeMap<String, String> filesMap = new TreeMap<String, String>();
    TreeMap<String, String> filesPathMap = new TreeMap<String, String>();
    for (File file : files) 
        if (file.isDirectory()) 
            String dirName = file.getName();
            dirsMap.put(dirName, dirName);
            dirsPathMap.put(dirName, file.getPath());
         else 
            final String fileName = file.getName();
            final String fileNameLwr = fileName.toLowerCase();
            // se ha um filtro de formatos, utiliza-o
            if (formatFilter != null) 
                boolean contains = false;
                for (int i = 0; i < formatFilter.length; i++) 
                    final String formatLwr = formatFilter[i].toLowerCase();
                    if (fileNameLwr.endsWith(formatLwr)) 
                        contains = true;
                        break;
                    
                
                if (contains) 
                    filesMap.put(fileName, fileName);
                    filesPathMap.put(fileName, file.getPath());
                
                // senao, adiciona todos os arquivos
             else 
                filesMap.put(fileName, fileName);
                filesPathMap.put(fileName, file.getPath());
            
        
    
    item.addAll(dirsMap.tailMap("").values());
    item.addAll(filesMap.tailMap("").values());
    path.addAll(dirsPathMap.tailMap("").values());
    path.addAll(filesPathMap.tailMap("").values());

    SimpleAdapter fileList = new SimpleAdapter(this, mList, R.layout.file_dialog_row, new String[] 
            ITEM_KEY, ITEM_IMAGE , new int[]  R.id.fdrowtext, R.id.fdrowimage );

    for (String dir : dirsMap.tailMap("").values()) 
        addItem(dir, R.drawable.folder);
    

    for (String file : filesMap.tailMap("").values()) 
        addItem(file, R.drawable.file);
    

    fileList.notifyDataSetChanged();

    setListAdapter(fileList);



private void addItem(String fileName, int imageId) 
    HashMap<String, Object> item = new HashMap<String, Object>();
    item.put(ITEM_KEY, fileName);
    item.put(ITEM_IMAGE, imageId);
    mList.add(item);


/**
 * Quando clica no item da lista, deve-se: 1) Se for diretorio, abre seus
 * arquivos filhos; 2) Se puder escolher diretorio, define-o como sendo o
 * path escolhido. 3) Se for arquivo, define-o como path escolhido. 4) Ativa
 * botao de selecao.
 */
@Override
protected void onListItemClick(ListView l, View v, int position, long id) 

    File file = new File(path.get(position));

    setSelectVisible(v);

    if (file.isDirectory()) 
        selectButton.setEnabled(false);
        if (file.canRead()) 
            lastPositions.put(currentPath, position);
            getDir(path.get(position));
            if (canSelectDir) 
                selectedFile = file;
                v.setSelected(true);
                selectButton.setEnabled(true);
            
         else 
            new AlertDialog.Builder(this).setIcon(R.drawable.icon)
                    .setTitle("[" + file.getName() + "] " + getText(R.string.cant_read_folder))
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() 

                        @Override
                        public void onClick(DialogInterface dialog, int which) 

                        
                    ).show();
        
     else 
        selectedFile = file;
        v.setSelected(true);
        selectButton.setEnabled(true);
    


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) 
        selectButton.setEnabled(false);

        if (layoutCreate.getVisibility() == View.VISIBLE) 
            layoutCreate.setVisibility(View.GONE);
            layoutSelect.setVisibility(View.VISIBLE);
         else 
            if (!currentPath.equals(ROOT)) 
                getDir(parentPath);
             else 
                return super.onKeyDown(keyCode, event);
            
        

        return true;
     else 
        return super.onKeyDown(keyCode, event);
    


/**
 * Define se o botao de CREATE e visivel.
 *
 * @param v
 */
private void setCreateVisible(View v) 
    layoutCreate.setVisibility(View.VISIBLE);
    layoutSelect.setVisibility(View.GONE);

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    selectButton.setEnabled(false);


/**
 * Define se o botao de SELECT e visivel.
 *
 * @param v
 */
private void setSelectVisible(View v) 
    layoutCreate.setVisibility(View.GONE);
    layoutSelect.setVisibility(View.VISIBLE);

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    selectButton.setEnabled(false);

谢谢大家的回答。

【问题讨论】:

【参考方案1】:

可能你必须在你的 android manifest.xml 中添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

【讨论】:

OP已经提到,他已经给了所有必要的权限。 是的。我已经做到了。谢谢普雷克。我想我刚刚找到了解决这个问题的方法。如果我像这样设置起始路径 getFilesDir().getAbsolutepath();它解决了这个问题,因为我可以看到我的文件和文件夹,但我仍然无法打开其他文件夹,如数据或 sdCard。

以上是关于无法读取文件夹。 (Android-文件-对话框)的主要内容,如果未能解决你的问题,请参考以下文章

移动硬盘文件或目录损坏且无法读取怎么解决

Android kotlin:无法读取类文件

Android Studio 无法安装更新(错误:无法读取或创建安装属性文件。)

无法解析 Android Studio 中的依赖项

unity3d android 下无法读取StreamingAssets 文件 路径都对啊

U盘移动硬盘等弹出 “文件或目录损坏且无法读取” 实测解决办法