在"/sdcard/"目录中列出文件返回null? [英] Listing files in "/sdcard/" returns 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]);
}
}
}
此代码将打印目录中的每个文件.它与root
,mnt
& 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屋!