你好,我正在开发基于Android gallery的视频播放器。我从gallery接收URI,并需要在播放视频时显示视频标题。因此,如果内容没有标题元数据,我需要显示视频文件名。如何获取视频内容文件名?
谢谢。
你好,我正在开发基于Android gallery的视频播放器。我从gallery接收URI,并需要在播放视频时显示视频标题。因此,如果内容没有标题元数据,我需要显示视频文件名。如何获取视频内容文件名?
谢谢。
以下是从url获取文件名的代码:
Uri u = Uri.parse("www.google.com/images/image.jpg");
File f = new File("" + u);
f.getName();
由于没有一个答案真正解决了问题,我在这里放上我的解决方案,希望它能有所帮助。
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();
}
}
}
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
val fileName = File(uri.path).name
以下代码适用于我从图库中选择图片的情况,它应该适用于任何文件类型。
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();
}
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)
{
}
int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String filename = cursor.getString(actual_image_column_index);
这是一个关于图片的示例。你可以根据自己的需求尝试同样的方法(比如视频)。
谢谢,祝你好运 :)
谷歌参考:
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)));
public String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
}
return result;
}
这里给你一个示例代码,可以从相册上传图片
要提取文件名/图片名
,请按照以下步骤操作
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
}
}
希望这能有所帮助
getPath()
是一个自定义函数,在你的代码片段中没有列出。 - sweisgerber.dev