你好 我開發的android庫的視頻播放器。 我從gallry收到URI。當播放視頻時我需要顯示視頻標題。 所以如果內容沒有標題元數據。我需要消除視頻文件名稱。 如何獲取視頻內容文件名?如何從URI獲取文件名
謝謝
你好 我開發的android庫的視頻播放器。 我從gallry收到URI。當播放視頻時我需要顯示視頻標題。 所以如果內容沒有標題元數據。我需要消除視頻文件名稱。 如何獲取視頻內容文件名?如何從URI獲取文件名
謝謝
這裏是從URL
獲取文件的名稱Uri u = Uri.parse("www.google.com/images/image.jpg");
File f = new File("" + u);
f.getName();
不適用於我的情況,它只會在網址末尾提供名稱,但在從Google驅動器內部文件或其他位置的文件中提取文檔時,會在最後一次返回字符串正斜槓。 – cammando 2017-03-09 11:39:16
int actual_image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String filename = cursor.getString(actual_image_column_index);
這是例如對於圖像的代碼。你可以嘗試同樣的東西滿足你的需求(視頻)。
感謝&祝您好運:)
我在這裏給你一個示例代碼,這將上傳圖片從畫廊
要提取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();
}
}
}
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)
{
}
下面的代碼對我的作品從畫廊圖像的情況下,它應該適用於任何文件類型。
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你可以找到詳細的解釋和更多。
所以你想要顯示的URI?或者用/分割並顯示最後一部分? – Mikhail 2010-11-24 02:42:35
我想顯示最後一部分。如果我顯示URI vALUE是/ external/video/media/3。但是3是id。不是文件名。 – byungkyu 2010-11-24 05:08:29
看看這個其他職位: [http://stackoverflow.com/questions/1105593/get-file-name-from-uri-string-in-c-sharp][1] [ 1]:http://stackoverflow.com/questions/1105593/get-file-name-from-uri-string-in-c-sharp – 2011-12-13 14:21:21