无法从文件路径中列出父目录。

huangapple go评论95阅读模式
英文:

Unable to list parent directory from file path

问题

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

    foldersList = new ArrayList<>();
    folderpath = new ArrayList<>();

    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    parseAllVideo();
    getUniqueFolders();
    adapter = new FolderAdapter(this, foldersList);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

private void parseAllVideo() {
    try {
        String[] proj = {
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA};

        Cursor videocursor = this.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
        if (videocursor != null) {
            if (videocursor.moveToFirst()) {
                int path_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                do {
                    String filepath = videocursor.getString(path_index);
                    String folder_path = MyUtils.getParentDirPath(filepath);
                    File file = new File(filepath);
                    if (file.exists()) {

                    }
                    folderpath.add(folder_path);
                } while (videocursor.moveToNext());
            }
            videocursor.close();
        }

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

private void getUniqueFolders() {
    List<String> newList = new ArrayList<>(new HashSet<>(folderpath));
    Collections.sort(newList);
    for (int i = 0; i < newList.size(); i++) {
        String folder_name = newList.get(i).substring(newList.get(i).lastIndexOf("/") + 1);
        FolderModel folder = new FolderModel(folder_name, newList.get(i));
        Log.d(TAG, "base file = " + folder);
        foldersList.add(folder);
    }
}

Images and screenshots have been removed from the translation.

英文:

In my code am trying to collect all video file in list and warp it inside there parent directory and display it inside recycler view, And while displaying i get a folder name 0 i don't understand where the 0 folder comes from.

And the 0 folder list all files inside my storage, And those file are perfectly warped inside their respective folder.

> and here is a screen shot of the folder zero.

无法从文件路径中列出父目录。

code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foldersList = new ArrayList&lt;&gt;();
folderpath = new ArrayList&lt;&gt;();
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
parseAllVideo();
getUniqueFolders();
adapter = new FolderAdapter(this, foldersList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void parseAllVideo() {
try {
String[] proj = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA};
Cursor videocursor = this.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if (videocursor != null) {
if (videocursor.moveToFirst()) {
int path_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
do {
String filepath = videocursor.getString(path_index);
String folder_path = MyUtils.getParentDirPath(filepath);
File file = new File(filepath);
if (file.exists()) {
}
folderpath.add(folder_path);
} while (videocursor.moveToNext());
}
videocursor.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void getUniqueFolders() {
List&lt;String&gt; newList = new ArrayList&lt;&gt;(new HashSet&lt;&gt;(folderpath));
Collections.sort(newList);
for (int i = 0; i &lt; newList.size(); i++) {
String folder_name = newList.get(i).substring(newList.get(i).lastIndexOf(&quot;/&quot;) + 1);
FolderModel folder = new FolderModel(folder_name, newList.get(i));
Log.d(TAG, &quot;base file = &quot; + folder);
foldersList.add(folder);
}
}![enter image description here](https://i.stack.imgur.com/rgQrz.jpg)

Here is what am trying to archive, the file in the internal folder is displayed as thumbnail by (glide, bit...) but with my code the 0 folder contains all file in my storage,

Here is what i want. 无法从文件路径中列出父目录。

答案1

得分: 0

根据@blackapps的评论,大多数Android设备上Environment.getExternalStorageDirectory().getAbsolutePath()的值为/storage/emulated/0。

当我检查了我的设备存储,一切都很正常,文件存储在内部存储位置。

英文:

as per @blackapps comment Environment.getExternalStorageDirectory().getAbsolutePath() value on most Android devices is /storage/emulated/0

and when i checked my device storage and everthing was fine and the file is stored in internal storage location

huangapple
  • 本文由 发表于 2020年8月28日 02:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63622553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定