我正在为我的学习课程创建CarApp,我想在其中将不同的数据保存到特定的汽车。我想在MediaTab中创建,保存和播放/观看不同的媒体类型。这些类型是图像,视频和音频。
因为我想在网格中显示图像以使其滑动,所以我想创建录制的视频的缩略图。创建缩略图的时间无关紧要。
现在我使用一种给我ImageSource的方法,我将其添加到cachedImage中,因为我收到了OutOfMemoryException。我现在的问题是图片是空的,我相信它来自位图转换。
我试图获取一个ImageSource,因为cachedImage需要Source,但这给了我同样的问题。
我也想看看路径,如果它出了点问题,但是控制台日志没有出现,那会更加令人困惑。
cs
using Android.Graphics;
using Android.Media;
using Android.Widget;
using FFImageLoading.Forms;
using myAuto.ContainerClasses;
using myAuto.Database;
using Plugin.Media;
using Plugin.Media.Abstractions;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Rg.Plugins.Popup.Services;
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.Xaml;
using Image = Xamarin.Forms.Image;
namespace myAuto
{
namespace View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CarMedia : ContentPage
{
public CarMedia()
{
InitializeComponent();
SQLite.SQLiteConnection conn = DependencyService.Get<DBConnect>().GetConnection();
List<DBMedia> medias = conn.Query<DBMedia>("SELECT * FROM DBMedia WHERE CID = ?", CarHelper.cars[CarHelper.cars.Count - 1]).ToList();
conn.Close();
if (medias.Count > 0)
{
int k = 0;
for (int i = 0; i < medias.Count; i++)
{
int j = 0;
while (j < 4 && k < medias.Count)
{
if (medias[i].format.Equals("Foto"))
{
var cachedImage = new CachedImage()
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
WidthRequest = 300,
HeightRequest = 100,
CacheDuration = TimeSpan.FromDays(30),
DownsampleToViewSize = true,
RetryCount = 0,
RetryDelay = 250,
BitmapOptimizations = false,
Source = medias[k].url
};
grGallery.Children.Add(cachedImage, j, i);
j++;
k++;
}
else if (medias[i].format.Equals("Video"))
{
var cachedImage = new CachedImage()
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
WidthRequest = 300,
HeightRequest = 100,
CacheDuration = TimeSpan.FromDays(30),
DownsampleToViewSize = true,
RetryCount = 0,
RetryDelay = 250,
BitmapOptimizations = true,
Source = GenerateThumbImage(medias[k].url.ToString(), 1).Source.ToString()
};
Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" + GenerateThumbImage(medias[k].url.ToString(), 1).Source.ToString());
grGallery.Children.Add(cachedImage, j, i);
j++;
k++;
}
else if (medias[i].format.Equals("Sound"))
{
j++;
k++;
}
else
{
DisplayAlert("Warnung!", "Dieses Format existiert nicht!", "Ok!");
}
}
}
}
butVideo.OnClickCommand = new Command(async () =>
{
fmMedia.Collapse(1);
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsTakeVideoSupported || !CrossMedia.Current.IsCameraAvailable)
{
await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
var file = await CrossMedia.Current.TakeVideoAsync(
new StoreVideoOptions
{
SaveToAlbum = true,
Directory = CarHelper.cars[CarHelper.cars.Count - 1].ToString(),
Quality = VideoQuality.High
}
);
if (file == null)
return;
/*
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});*/
InsertMediaIntoDB("Video", file.Path.ToString());
await DisplayAlert("aa", file.Path.ToString(), "ok");
file.Dispose();
});
butPhoto.OnClickCommand = new Command(async () =>
{
fmMedia.Collapse(1);
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsTakePhotoSupported || !CrossMedia.Current.IsCameraAvailable)
{
await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync
(
new StoreCameraMediaOptions
{
SaveToAlbum = true,
Directory = CarHelper.cars[CarHelper.cars.Count - 1].ToString()
}
);
if (file == null)
return;
/*
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});*/
InsertMediaIntoDB("Foto", file.Path.ToString());
file.Dispose();
});
butSound.OnClickCommand = new Command(async () =>
{
await PopupNavigation.PushAsync(new CarMediaSound());
});
}
public void InsertMediaIntoDB(String format, String url)
{
SQLiteConnection conn = DependencyService.Get<DBConnect>().GetConnection();
DBMedia medias = new DBMedia();
medias.CID = CarHelper.cars[CarHelper.cars.Count - 1];
medias.format = format;
medias.url = url;
int x = 0;
try
{
x = conn.Insert(medias);
}
catch (Exception ex)
{
throw ex;
}
if (x == 0)
{
DisplayAlert("Warnung!", "Fehler beim hinzufügen in die Datenbank!", "Ok!");
}
conn.Close();
}
public Image GenerateThumbImage(String url, long usecond)
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.SetDataSource(url, new Dictionary<String, String>());
Bitmap bitmap = retriever.GetFrameAtTime(usecond);
//Image image = (Image)retriever.GetFrameAtTime(usecond);
if (bitmap != null)
{
Image i = new Image();
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] bitmapData = stream.ToArray();
i.Source = ImageSource.FromStream(() => new MemoryStream(bitmapData));
return i;
}
return null;
}
}
}
}
xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="myAuto.View.CarMedia"
xmlns:c="clr-namespace:myAuto.Controls"
BackgroundColor="#515151">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0" Grid.RowSpan="6"
Grid.ColumnSpan="5">
<ScrollView>
<Grid x:Name="grGallery">
</Grid>
</ScrollView>
</StackLayout>
<c:FloatingMenu x:Name="fmMedia" Grid.Column="4" Grid.Row="0"
Grid.RowSpan="6" Margin="0, 0, 10, 10" BGColor="#515151"
OpenIcon="icons8plus96.png" CloseIcon="icons8cancel96.png">
<c:FloatingButton x:Name="butVideo" BGColor="#515151"
IconSrc="icons8videocamera96.png" CornerRadius="15"/>
<c:FloatingButton x:Name="butPhoto" BGColor="#515151"
IconSrc="icons8camerafilled96.png" CornerRadius="15"/>
<c:FloatingButton x:Name="butSound" BGColor="#515151"
IconSrc="icons8micro96.png" CornerRadius="15"/>
</c:FloatingMenu>
</Grid>
</ContentPage.Content>
</ContentPage>
最后,我只想让缩略图显示在网格中,也许上面有一个小播放按钮,以便用户可以看到其视频。
我真的很感谢各种帮助,例如使用不同的插件,获取示例代码或只是快速的解决方案。