如何从URI获取文件名

时间:2010-11-24 02:35:11

标签: android media-player

您好 我正在使用android gallery开发视频播放器。 我从胆汁中收到URI。我需要在播放视频时显示视频标题。 所以,如果内容没有标题元数据。我需要取消视频文件名。 如何获取视频内容文件名?

谢谢

9 个答案:

答案 0 :(得分:23)

以下是从url

获取文件名的代码
Uri u = Uri.parse("www.google.com/images/image.jpg");

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

f.getName();

答案 1 :(得分:4)

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

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
String filename = cursor.getString(actual_image_column_index);

这是Image的示例。你可以根据自己的需要尝试同样的事情(视频)。

谢谢&祝你好运:)

答案 4 :(得分:1)

try
        {
            String uriString = "http://somesite.com/video.mp4";
            URI uri = new URI(uriString);

            URL videoUrl = uri.toURL();
            File tempFile = new File(videoUrl.getFile());
            String fileName = tempFile.getName();
        }
        catch (Exception e)
        {

        }

答案 5 :(得分:1)

对于Kotlin来说,很简单:

val fileName = File(uri.path).name

答案 6 :(得分:0)

以下代码适用于我来自图库的图片,它适用于任何文件类型。

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

Here您可以找到详细解释等等。

答案 7 :(得分:0)

谷歌参考:

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

        /*
         * 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)));

答案 8 :(得分:-2)

在这里,我将为您提供一个示例代码,用于从库中上传图像

要提取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
}
}

希望这会有所帮助