在"/sdcard/"目录中列出文件返回null? [英] Listing files in "/sdcard/" returns null?

查看:139
本文介绍了在"/sdcard/"目录中列出文件返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要访问Android手机内部存储(例如下载文件夹)的应用.但是,当我尝试列出文件时

I have an app that needs access to an android phone's internal storage, such as the Download folder. However when I try to list the files it alimport

java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        File root = new File("/"); // Works
        File mnt = new File("/mnt/"); // Works
        File mnt2 = new File("/mnt/m_internal_storage"); // Crashes
        File cus = new File("/custom/"); // Works
        File f = new File("/sdcard/"); // Crashes
        String[] s =  f.list();
        for (int x = 0; x < s.length; x++) {
            Log.println(Log.ASSERT, "#", s[x]);
        }
    }
}

此代码将打印目录中的每个文件.它与rootmnt& cus但尝试获取f&的列表时会崩溃mnt2,将null数组返回到s.如何访问内部文件存储?

This code will print every file in the directory. It works with root, mnt & cus but crashes when it tries to get the list of f & mnt2, returning a null array to s. How do I access the internal file storage?

我也尝试过在大多数情况下使用Environment的功能,甚至还启用了外部存储"的读/写"权限.

I have also tried using Environment's functions with most failing and I even have Read/Write permissions for External Storage enabled.

针对7.0进行编译. 定位API 23. 最小SDK API 18.

Compiling for 7.0. Targeting API 23. Min sdk API 18.

推荐答案

我有一个需要访问android手机内部存储的应用程序,例如下载"文件夹.

I have an app that needs access to an android phone's internal storage, such as the Download folder.

用户看到的Download文件夹位于Android SDK调用的外部存储.

The Download folder that users see is on what the Android SDK calls external storage.

如何访问内部文件存储?

How do I access the internal file storage?

使用Environment.getExternalStorageDirectory(). 从不硬编码路径.例如,行星表面上几乎没有设备具有/mnt/m_internal_storage/custom//sdacard/.

Use Environment.getExternalStorageDirectory(). Never hardcode paths. For example, vanishingly few devices on the face of the planet have /mnt/m_internal_storage, /custom/, or /sdacard/.

我还尝试了大多数情况下使用环境功能,甚至启用了外部存储"的读/写"权限.

I have also tried using Environment's functions with most failing and I even have Read/Write permissions for External Storage enabled.

也许您没有实现运行时权限.

这篇关于在"/sdcard/"目录中列出文件返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆