为什么通过相册枚举 returns 错误的文件名?
Why enumerating through the Camera Roll returns wrong file name?
我在 1520 上用 Lumia Selfie 应用程序拍了张照片。照片存储在 "Camera Roll"。
然后我使用 PhotoChooserTask 选择了图片。所选图片的文件名是:
C:\Data\Users\Public\Pictures\Camera Roll\WP_20150211_13_52_40_Selfie.jpg
在我的代码中,我保存了相册和文件名,即:
文件名 = "Camera Roll\WP_20150211_13_52_40_Selfie.jpg"
后来,我尝试从相机胶卷中检索照片。我使用以下代码来枚举相册相册:
public BitmapImage GetPicture( string FileName )
{
PictureAlbum cameraRoll = null;
PictureAlbum savedPictures = null;
PictureAlbum samplePictures = null;
PictureAlbum favoritePictures = null;
int index = FileName.IndexOf("\", StringComparison.Ordinal);
string albumName = FileName.Remove( index, FileName.Length - index );
string fileName = FileName.Remove( 0, index + 1 );
foreach ( MediaSource source in MediaSource.GetAvailableMediaSources() )
{
if ( source.MediaSourceType == MediaSourceType.LocalDevice )
{
var ml = new MediaLibrary( source );
PictureAlbumCollection allAlbums = ml.RootPictureAlbum.Albums;
foreach ( PictureAlbum album in allAlbums )
{
if ( album.Name == "Camera Roll" )
{
cameraRoll = album;
}
else if ( album.Name == "Saved Pictures" )
{
savedPictures = album;
}
else if ( album.Name == "Sample Pictures" )
{
samplePictures = album;
}
else if ( album.Name == "Favorite Pictures" )
{
favoritePictures = album;
}
}
}
}
PictureAlbum Album;
switch ( albumName )
{
case "Camera Roll":
Album = cameraRoll;
break;
case "Saved Pictures":
Album = savedPictures;
break;
case "Sample Pictures":
Album = samplePictures;
break;
case "Favorite Pictures":
Album = favoritePictures;
break;
default:
Album = null;
break;
}
if ( Album == null )
{
return new BitmapImage();
}
var b = new BitmapImage();
//foreach ( Picture p in Album.Pictures )
foreach ( Picture p in Album.Pictures.Take( Album.Pictures.Count ) )
{
Debug.WriteLine( p.Name + " " + fileName );
if ( fileName != p.Name )
{
continue;
}
b.SetSource( p.GetThumbnail() );
break;
}
return b;
}
在输出 windows 中,我看到 p.Name 现在返回为:
"Taken with Lumia Selfie" 而不是预期的 "WP_20150211_13_52_40_Selfie.jpg"。
谁能解释为什么会这样,and/or我做错了什么?
谢谢
来自本网站上的 post 前辈:
Picture.Name 不是文件名。它是创建者可以分配给图片的元数据 "title"。您可以使用 MediaLibraryExtensions.GetPath(picture) 来获取文件路径:
顺便说一句,出于所有实际目的,MediaLibrary 现在已弃用 API。我建议您开始使用 StorageFolder 和 StorageFile API 来访问文件。
我在 1520 上用 Lumia Selfie 应用程序拍了张照片。照片存储在 "Camera Roll"。
然后我使用 PhotoChooserTask 选择了图片。所选图片的文件名是: C:\Data\Users\Public\Pictures\Camera Roll\WP_20150211_13_52_40_Selfie.jpg
在我的代码中,我保存了相册和文件名,即: 文件名 = "Camera Roll\WP_20150211_13_52_40_Selfie.jpg"
后来,我尝试从相机胶卷中检索照片。我使用以下代码来枚举相册相册:
public BitmapImage GetPicture( string FileName )
{
PictureAlbum cameraRoll = null;
PictureAlbum savedPictures = null;
PictureAlbum samplePictures = null;
PictureAlbum favoritePictures = null;
int index = FileName.IndexOf("\", StringComparison.Ordinal);
string albumName = FileName.Remove( index, FileName.Length - index );
string fileName = FileName.Remove( 0, index + 1 );
foreach ( MediaSource source in MediaSource.GetAvailableMediaSources() )
{
if ( source.MediaSourceType == MediaSourceType.LocalDevice )
{
var ml = new MediaLibrary( source );
PictureAlbumCollection allAlbums = ml.RootPictureAlbum.Albums;
foreach ( PictureAlbum album in allAlbums )
{
if ( album.Name == "Camera Roll" )
{
cameraRoll = album;
}
else if ( album.Name == "Saved Pictures" )
{
savedPictures = album;
}
else if ( album.Name == "Sample Pictures" )
{
samplePictures = album;
}
else if ( album.Name == "Favorite Pictures" )
{
favoritePictures = album;
}
}
}
}
PictureAlbum Album;
switch ( albumName )
{
case "Camera Roll":
Album = cameraRoll;
break;
case "Saved Pictures":
Album = savedPictures;
break;
case "Sample Pictures":
Album = samplePictures;
break;
case "Favorite Pictures":
Album = favoritePictures;
break;
default:
Album = null;
break;
}
if ( Album == null )
{
return new BitmapImage();
}
var b = new BitmapImage();
//foreach ( Picture p in Album.Pictures )
foreach ( Picture p in Album.Pictures.Take( Album.Pictures.Count ) )
{
Debug.WriteLine( p.Name + " " + fileName );
if ( fileName != p.Name )
{
continue;
}
b.SetSource( p.GetThumbnail() );
break;
}
return b;
}
在输出 windows 中,我看到 p.Name 现在返回为: "Taken with Lumia Selfie" 而不是预期的 "WP_20150211_13_52_40_Selfie.jpg"。
谁能解释为什么会这样,and/or我做错了什么?
谢谢
来自本网站上的 post 前辈:
Picture.Name 不是文件名。它是创建者可以分配给图片的元数据 "title"。您可以使用 MediaLibraryExtensions.GetPath(picture) 来获取文件路径:
顺便说一句,出于所有实际目的,MediaLibrary 现在已弃用 API。我建议您开始使用 StorageFolder 和 StorageFile API 来访问文件。