Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从URI获取文件名_Android_Media Player - Fatal编程技术网

Android 如何从URI获取文件名

Android 如何从URI获取文件名,android,media-player,Android,Media Player,你好 我正在用android gallery开发视频播放器。 我从加利那里收到了URI。我需要在播放视频时显示视频标题。 所以如果内容没有标题元数据。我需要播放视频文件名。 如何获取视频内容文件名 谢谢以下是从url获取文件名的代码 Uri u = Uri.parse("www.google.com/images/image.jpg"); File f = new File("" + u); f.getName(); 这是图像的示例。你可以根据自己的需要尝试同样的东西(视频) 谢谢&祝你好

你好 我正在用android gallery开发视频播放器。 我从加利那里收到了URI。我需要在播放视频时显示视频标题。 所以如果内容没有标题元数据。我需要播放视频文件名。 如何获取视频内容文件名


谢谢

以下是从url获取文件名的代码

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

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

f.getName();
这是图像的示例。你可以根据自己的需要尝试同样的东西(视频)


谢谢&祝你好运:)

这里我给你一个示例代码,可以从图库上传图片

要提取
filename/Imagename
,请执行以下操作

File f = new File(selectedPath);
System.out.println("Image name is   ::" + f.getName());//get name of file

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == YourActivity.RESULT_OK) {
Uri selectedImageUri = data.getData();
String selectedPath = getPath(selectedImageUri);
File f = new File(selectedPath);
System.out.println("Image name is   ::" + f.getName());//get name of file
}
}

希望这会有帮助

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

     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();
                }
            }
        }

下面的代码适用于我的情况下,从画廊的图像,它应该适用于任何文件类型

    String fileName = "default_file_name";
    Cursor returnCursor =
            getContentResolver().query(YourFileUri, null, null, null, null);
    try {
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        returnCursor.moveToFirst(); 
        fileName = returnCursor.getString(nameIndex);
        LOG.debug(TAG, "file name : " + fileName);
    }catch (Exception e){
        LOG.error(TAG, "error: ", e);
        //handle the failure cases here
    } finally {
        returnCursor.close();
    }

您可以找到详细的解释和更多信息。

如果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));

对于kotlin,只需简单地:

val fileName = File(uri.path).name

谷歌参考:


那么您想显示URI吗?还是按/拆分并显示最后一部分?我想显示最后一部分。如果我显示URI值为/external/video/media/3。但3是id,不是文件名。请看另一篇帖子:[[1]:谷歌的答案:
getPath()
是一个自定义函数,未在代码段中列出。在我的情况下不起作用。它只在url末尾给出名称,但在从Google驱动器内的文件或其他地方获取文档时,它会在最后一个正斜杠后返回字符串。有人对答案投了反对票。请您解释一下原因好吗?@Grsmto、 很抱歉,但是你在写评论之前有没有试着运行代码?我想没有,因为我刚刚检查了你的示例url,它返回了正确的结果-file.mp3I可能是错的,我意识到我的Android Studio调试器的行为真的很奇怪…有一些以前版本的旧缓存,所有的…我将删除我的答案。
        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));
val fileName = File(uri.path).name
        /*
         * Get the file's content URI from the incoming Intent,
         * then query the server app to get the file's display name
         * and size.
         */
        Uri returnUri = returnIntent.getData();
        Cursor returnCursor =
                getContentResolver().query(returnUri, 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);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        TextView nameView = (TextView) findViewById(R.id.filename_text);
        TextView sizeView = (TextView) findViewById(R.id.filesize_text);
        nameView.setText(returnCursor.getString(nameIndex));
        sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));