안드로이드 디렉토리에 파일을 나열하는 방법?
지금까지 내 코드는 다음과 같습니다.
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
AssetManager mgr = getAssets();
try {
String list[] = mgr.list(path);
Log.e("FILES", String.valueOf(list.length));
if (list != null)
for (int i=0; i<list.length; ++i)
{
Log.e("FILE:", path +"/"+ list[i]);
}
} catch (IOException e) {
Log.v("List error:", "can't list" + path);
}
그러나 그 디렉토리에 파일이 있지만 list.length = 0 ... 어떤 아이디어를 반환합니까?
이 시도:
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
방금 그것을 발견했습니다.
new File("/sdcard/").listFiles()
없는 경우 null을 반환합니다.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
AndroidManifest.xml 파일에서 설정하십시오.
음, AssetManager
내 목록의 파일을 assets
사용자의 APK 파일의 내부 폴더에 있습니다. 위의 예에서 나열하려는 것은 [apk] / assets / sdcard / Pictures입니다.
assets
응용 프로그램 내부의 폴더 안에 그림을 넣고 Pictures
디렉토리에 있으면 사진을 찍을 수 mgr.list("/Pictures/")
있습니다.
반면, APK 파일 외부의 SD 카드에 파일이 있으면 Pictures
폴더에 다음 File
과 같이 사용 합니다.
File file = new File(Environment.getExternalStorageDirectory(), "Pictures");
File[] pictures = file.listFiles();
...
for (...)
{
log.e("FILE:", pictures[i].getAbsolutePath());
}
문서의 관련 링크 :
File
Asset Manager
In addition to all the answers above:
If you are on Android 6.0+ (API Level 23+) you have to explicitly ask for permission to access external storage. Simply having
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in your manifest won't be enough. You also have actively request the permission in your activity:
//check for permission
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
//ask for permission
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_PERMISSION_CODE);
}
I recommend reading this: http://developer.android.com/training/permissions/requesting.html#perm-request
Your path
is not within the assets folder. Either you enumerate files within the assets folder by means of AssetManager.list()
or you enumerate files on your SD card by means of File.list()
String[] listOfFiles = getActivity().getFilesDir().list();
or
String[] listOfFiles = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS).list();
Try this:
public class GetAllFilesInDirectory {
public static void main(String[] args) throws IOException {
File dir = new File("dir");
System.out.println("Getting all files in " + dir.getCanonicalPath() + " including those in subdirectories");
List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
System.out.println("file: " + file.getCanonicalPath());
}
}
}
There are two things that could be happening:
- You are not adding
READ_EXTERNAL_STORAGE
permission to yourAndroidManifest.xml
- You are targeting Android 23 and you're not asking for that permission to the user. Go down to Android 22 or ask the user for that permission.
Try these
String appDirectoryName = getResources().getString(R.string.app_name);
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getResources().getString(R.string.app_name));
directory.mkdirs();
File[] fList = directory.listFiles();
int a = 1;
for (int x = 0; x < fList.length; x++) {
//txt.setText("You Have Capture " + String.valueOf(a) + " Photos");
a++;
}
//get all the files from a directory
for (File file : fList) {
if (file.isFile()) {
list.add(new ModelClass(file.getName(), file.getAbsolutePath()));
}
}
참고URL : https://stackoverflow.com/questions/8646984/how-to-list-files-in-an-android-directory
'programing tip' 카테고리의 다른 글
모든 플랫폼에서 이온 모드에서만 앱을 세로 모드로 제한하는 방법은 무엇입니까? (0) | 2020.07.26 |
---|---|
PHP7 : ext-dom 문제 설치 (0) | 2020.07.26 |
DataTable : 항목 표시 드롭 다운을 숨기고 검색 상자는 유지 (0) | 2020.07.26 |
유효한 IPv6 주소와 일치하는 정규식 (0) | 2020.07.26 |
Vim에서 f 및 t 명령은 무엇을합니까? (0) | 2020.07.26 |