如何从URI获取文件名

byu*_*kyu 13 android media-player

你好,我正在开发Android画廊的视频播放器.我从胆汁中收到URI.我需要在播放视频时显示视频标题.所以,如果内容没有标题元数据.我需要取消视频文件名.如何获取视频内容文件名?

谢谢

far*_*ath 23

这是从url获取文件名的代码

Uri u = Uri.parse("www.google.com/images/image.jpg");

File f = new File("" + u);

f.getName();
Run Code Online (Sandbox Code Playgroud)


ali*_*dro 7

由于没有一个答案能真正解决问题,我把我的解决方案放在这里,希望它会有所帮助。

     String fileName;
     if (uri.getScheme().equals("file")) {
            fileName = uri.getLastPathSegment();
        } else {
            Cursor cursor = null;
            try {
                cursor = getContentResolver().query(uri, new String[]{
                        MediaStore.Images.ImageColumns.DISPLAY_NAME
                }, null, null, null);

                if (cursor != null && cursor.moveToFirst()) {
                    fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));
                    Log.d(TAG, "name is " + fileName);
                }
            } finally {

                if (cursor != null) {
                    cursor.close();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)


not*_*guy 5

如果 uri 是内容提供者 uri,则这就是您检索文件信息的方式。

        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        /*
         * Get the column indexes of the data in the Cursor,
         * move to the first row in the Cursor, get the data,
         * and display it.
         */
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        cursor.moveToFirst();
        nameView.setText(cursor.getString(nameIndex));
Run Code Online (Sandbox Code Playgroud)

https://developer.android.com/training/secure-file-sharing/retrieve-info.html