I want to use MuPDF reader for my Xamarin Android project .
I am attempting to view a PDF in my relative layout
Here is my Relative layout code
<RelativeLayout
android:id="#+id/mupdf_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
and here is main acitivity
SetContentView(Resource.Layout.Main);
RelativeLayout mupdfWrapper = FindViewById<RelativeLayout>(Resource.Id.mupdf_wrapper);
MuPDFCore core = new MuPDFCore(this, "test.pdf");
MuPDFReaderView reader = new MuPDFReaderView(this);
reader.Adapter = new MuPDFPageAdapter(this, new FilePicker.IFilePickerSupport() , core);
mupdfWrapper.AddView(reader);
mupdfWrapper.AddView(reader);
But i am getting Error here
"cannot create an istance of the abstract class or interface 'File picker .iflepickersupport"
Can anyone help me resolve this issue please.
Thanks in advance.
If you don't have use of FilePicker.IFilePickerSupport() then set it null
like
reader.Adapter = new MuPDFPageAdapter(this, null , core);
and second thing is that your code is very helpful for me there you are facing problem but your problem is my solution in my project so thank you.
and try it it will work and i am using it in my code it's work for me.
and last this thing is that sorry for my english.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
File fileToDisplay = (File)fileFromAsset(this, "test.pdf");
fileToDisplay.SetWritable(true);
RelativeLayout mupdfWrapper = FindViewById<RelativeLayout>(Resource.Id.mupdf_wrapper);
MuPDFCore core = new MuPDFCore(this, fileToDisplay.AbsolutePath);
MuPDFReaderView reader = new MuPDFReaderView(this);
MuPDFPageAdapter adapter = new MuPDFPageAdapter(this, null, core);
reader.SetAdapter(adapter);
mupdfWrapper.AddView(reader);
}
private object fileFromAsset(Context context, string assetName)
{
File outFile = new File(context.CacheDir, assetName);
copy(context.Assets.Open(assetName), outFile);
return outFile;
}
private void copy(Stream inputStream, File output)
{
OutputStream outputStream = null;
var bufferedInputStream = new BufferedInputStream(inputStream);
try
{
outputStream = new FileOutputStream(output);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = bufferedInputStream.Read(bytes)) != -1)
{
outputStream.Write(bytes, 0, read);
}
}
finally
{
try
{
if (inputStream != null)
{
inputStream.Close();
inputStream.Dispose();
inputStream = null;
}
}
finally
{
if (outputStream != null)
{
outputStream.Close();
outputStream.Dispose();
outputStream = null;
}
}
}
}
Related
I am developing a Xamarin app which retrives info from DB, take/choose photo and upload them to remote server, display this images from the remote server and the user can delete them by tap on and press a button and download the images from the remote server to the local device.
Everything works without problem, but when I download the image and after I go to the gallery for check it, the image does not appear, whereas I can see it and open in the file explorer. When I reboot the phone, the image appear in the gallery.
Below my current button download method:
private void button_download_image_Clicked(object sender, EventArgs e)
{
Uri image_url_format = new Uri(image_url);
WebClient webClient = new WebClient();
try
{
byte[] bytes_image = webClient.DownloadData(image_url_format);
Stream image_stream = new MemoryStream(bytes_image);
string dest_folder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString();
string file_name = System.IO.Path.GetFileName(image_url_format.LocalPath);
string dest_path = System.IO.Path.Combine(dest_folder, file_name);
using (var fileStream = new FileStream(dest_path, FileMode.Create, FileAccess.Write))
{
image_stream.CopyTo(fileStream);
}
}
catch (Exception ex)
{
DisplayAlert("Error", ex.ToString(), "OK");
}
DisplayAlert("Alert", "Download completed!", "OK");
}
I tried in another device, but I got the same behavior.
Probably there is a sort of thing which does not refresh the gallery.
Any idea how to force the gallery to refresh or something similar?
You need to refresh your gallery after inserting or deleting any pictures in storage.
You can try this.
var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(dest_path)));
Xamarin.Forms.Forms.Context.SendBroadcast(mediaScanIntent);
Add these lines below your code.
Make it like
private void button_download_image_Clicked(object sender, EventArgs e)
{
Uri image_url_format = new Uri(image_url);
WebClient webClient = new WebClient();
try
{
byte[] bytes_image = webClient.DownloadData(image_url_format);
Stream image_stream = new MemoryStream(bytes_image);
string dest_folder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString();
string file_name = System.IO.Path.GetFileName(image_url_format.LocalPath);
string dest_path = System.IO.Path.Combine(dest_folder, file_name);
using (var fileStream = new FileStream(dest_path, FileMode.Create, FileAccess.Write))
{
image_stream.CopyTo(fileStream);
}
var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(dest_path)));
//for old xamarin forms version
Xamarin.Forms.Forms.Context.SendBroadcast(mediaScanIntent);
//for new xamarin forms version
//Android.App.Application.SendBroadcast(mediaScanIntent);
}
catch (Exception ex)
{
DisplayAlert("Error", ex.ToString(), "OK");
return;
}
DisplayAlert("Alert", "Download completed!", "OK");
}
You need to just refresh the file you have downloaded. It's helpful.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}else{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
Make sure required permission given on both platforms.
Use in your class:
bool success = await DependencyService.Get<IPhotoLibrary>().SavePhotoAsync(data, folder, filename);
Common Interface
public interface IPhotoLibrary
{
Task<bool> SavePhotoAsync(byte[] data, string folder, string filename);
}
In Android service
public async Task<bool> SavePhotoAsync(byte[] data, string folder, string filename)
{
try
{
File picturesDirectory = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
File folderDirectory = picturesDirectory;
if (!string.IsNullOrEmpty(folder))
{
folderDirectory = new File(picturesDirectory, folder);
folderDirectory.Mkdirs();
}
using (File bitmapFile = new File(folderDirectory, filename))
{
bitmapFile.CreateNewFile();
using (FileOutputStream outputStream = new FileOutputStream(bitmapFile))
{
await outputStream.WriteAsync(data);
}
// Make sure it shows up in the Photos gallery promptly.
MediaScannerConnection.ScanFile(MainActivity.Instance,
new string[] { bitmapFile.Path },
new string[] { "image/png", "image/jpeg" }, null);
}
}
catch (System.Exception ex)
{
return false;
}
return true;
}
In iOS service:
public Task<bool> SavePhotoAsync(byte[] data, string folder, string filename)
{
NSData nsData = NSData.FromArray(data);
UIImage image = new UIImage(nsData);
TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>();
image.SaveToPhotosAlbum((UIImage img, NSError error) =>
{
taskCompletionSource.SetResult(error == null);
});
return taskCompletionSource.Task;
}
also you can refer this one just to save an image and to reflect it in media, no need to use skiasharp for that. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/bitmaps/saving
Hope this may resolve your issue.
Refer to Blu's answer,
I changed this Xamarin.Forms.Forms.Context.SendBroadcast(mediaScanIntent); to Android.App.Application.Context.SendBroadcast(mediaScanIntent); and all works.
I am developing a project using c#.in my project i should get the camera stream .So i use onvif to get the stream but i faced a problem .some of my camera can't support onvif and they support RTSP,and i have to use rtsp in my project to :
Here is my onvif code to get the camera stream :
async void StartCameraAfterLogin()
{
//Dooooorbin
await ProcessConnect("http://172.16.0.52/onvif/device_service"
, Properties.Settings.Default.CameraUserName, Properties.Settings.Default.CameraPassword);
}
async Task ProcessConnect(string addr, string name, string pass)
{
//Release();
//ctrlInfo.Content = new ProgressControl();
var account = new System.Net.NetworkCredential(name, pass);
NvtSessionFactory factory = new NvtSessionFactory(account);
////This is syncronouse ui blokking call
//var session = factory.CreateSession(new Uri(addr);
try
{
var session = factory.CreateSession(new Uri(addr));
ss = session;
await FillInfoArea(session);
}
catch (Exception err)
{
Algoa.Utilities.Logger.ToDebug(err);
//ctrlInfo.Content = new ErrorControl(err);
}
}
string profie;
private async Task FillInfoArea(INvtSession session)
{
var profInfo = new ProfieInfoControl();
try
{
var streamData = await profInfo.Init(session);
//sp.Children.Add(profInfo);
profie = profInfo.valueToken.Text;
InitVideoControl(streamData.Item1, streamData.Item4, session.credentials);
//InitPtzControl(session, streamData.Item2, streamData.Item3);
}
catch (Exception err)
{
Algoa.Utilities.Logger.ToDebug(err);
}
//ctrlInfo.Content = sp;
}
private void InitVideoControl(string suri, System.Windows.Size size, System.Net.NetworkCredential networkCredential)
{
InitPlayer(suri, networkCredential, size);
}
public void InitPlayer(string videoUri, NetworkCredential account, System.Windows.Size sz = default(System.Windows.Size))
{
player = new HostedPlayer();
playerDisposable.Add(player);
if (sz != default(System.Windows.Size))
videoBuff = new VideoBuffer((int)sz.Width, (int)sz.Height);
else
{
videoBuff = new VideoBuffer(720, 576);
}
player.SetVideoBuffer(videoBuff);
MediaStreamInfo.Transport transp = MediaStreamInfo.Transport.Udp;
MediaStreamInfo mstrInfo = null;
if (account != null)
{
mstrInfo = new MediaStreamInfo(videoUri, transp, new UserNameToken(account.UserName, account.Password));//, transp, null);
}
else
{
mstrInfo = new MediaStreamInfo(videoUri, transp);
}
playerDisposable.Add(
player.Play(mstrInfo, this)
);
InitPlayback(videoBuff, true);
//{ // playing controls
// btnPause.Click += play;
// btnResume.Click += play;
//}
}
How can i convert my onvif url to rtsp url ?Is there any solution or i should change all part of my code?
I am new in camera stream coding .
I downloaded the OZEKI-SDK
private IIPCamera _camera;
private DrawingImageProvider _imageProvider = new DrawingImageProvider();
private MediaConnector _connector = new MediaConnector();
private VideoViewerWF _videoViewerWF1;
public Form1()
{
InitializeComponent();
// Create video viewer UI control
_videoViewerWF1 = new VideoViewerWF();
_videoViewerWF1.Name = "videoViewerWF1";
_videoViewerWF1.Size = panel1.Size;
panel1.Controls.Add(_videoViewerWF1);
// Bind the camera image to the UI control
_videoViewerWF1.SetImageProvider(_imageProvider);
}
// Connect camera video channel to image provider and start
private void connectBtn_Click(object sender, EventArgs e)
{
_camera = IPCameraFactory.GetCamera("rtsp://192.168.113.150:554/ufirststream", "root", "pass");
_connector.Connect(_camera.VideoChannel, _imageProvider);
_camera.Start();
_videoViewerWF1.Start();
}
As a another solution i finally used Accord.net library to capture rtsp stream.
I'm currently working on an audiobackend for my application, which runs on Mono/.NET. Therefore I found, that SFML is plattformindependent, so it suits. But there is no mp3 support because of licensing.
So I wanted to add up the mp3 capability for myself in C#. I use SFML.Net 2.1 with Mp3Sharp (http://www.robburke.net/mle/mp3sharp/). At first I tried this:
Mp3Stream stream = new Mp3Stream("D:\\tmp\\test.mp3");
Music music = new Music(stream);
music.play();
But that didn't work. Exception:
AudioCuesheetEditor.AudioBackend.AudioManagerSFML: FATAL | SFML.LoadingFailedException: Failed to load music from memory
at SFML.Audio.Music..ctor(Stream stream)
at AudioCuesheetEditor.AudioBackend.AudioManagerSFML.setMusic() in d:\tmp\AudioCuesheetEditor\src\AudioCuesheetEditor\AudioBackend\AudioManagerSFML.cs
So I thought about doing as it is done in the examples (https://github.com/LaurentGomila/SFML/wiki/Source:-MP3-Player):
I made a class SFMLMp3Stream which extends SoundStream:
public class SFMLMp3Stream : SoundStream
{
private Mp3Stream stream;
public SFMLMp3Stream()
{
stream = null;
}
public Boolean openFromFile(String _filename)
{
//TODO:Check file existence
stream = new Mp3Stream(_filename);
Initialize((uint)stream.ChannelCount,(uint) stream.Frequency);
return true;
}
#region implemented abstract members of SoundStream
protected override bool OnGetData(out short[] samples)
{
if (stream != null)
{
byte[] buffer = new byte[512];
short[] retVal = new short[512];
stream.Read(buffer,0,buffer.Length);
Buffer.BlockCopy(buffer,0,retVal,0,buffer.Length);
samples = retVal;
return true;
}
else
{
samples = null;
return false;
}
}
protected override void OnSeek(TimeSpan timeOffset)
{
if (stream != null)
{
stream.Seek((long)timeOffset.TotalMilliseconds, SeekOrigin.Current);
}
}
#endregion
}
When I try now this:
musicStream = new SFMLMp3Stream();
musicStream.openFromFile(this.objProgram.getObjCuesheet().getAudiofilePath(true));
music = new Music(musicStream);
I get the compiler error, that Music can not be initiatet from SFMLMp3Stream (because it is no string and no System.IO.Stream. How can I extend SFML for Mp3 usage? Has anybody done this so far?
Thanks for your help
Sven
I've got a windows form with save/loading of XML files and it asks the user where they want to save/load it. My problem is I dont know how to change this method to load the file from where the user wants and not where the streamreader specifies.
The code below is of my button and LoadValues Method.
private void Edittask_loadbuttonClick(
object sender, EventArgs e)
{
Stream myStream = null;
var sFile1 = new OpenFileDialog();
sFile1.InitialDirectory = "c:\\";
sFile1.Filter = "xml files (*.xml)|*.xml";
sFile1.FilterIndex = 2;
sFile1.RestoreDirectory = true;
if (sFile1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = sFile1.OpenFile()) != null)
{
using (myStream)
{
var v = LoadValues();
this.load_task1_name.Text =
v.task1_name;
this.load_task1_desc.Text =
v.task1_desc;
this.load_task1_date.Value =
v.task1_date;
this.load_checkbox.Checked =
v.task1_checkbox;
}
}
}
catch (Exception ex)
{
MessageBox.Show(
"Error: Could not read file from disk. Original error: " +
ex.Message);
}
}
}
public Values LoadValues()
{
var serializer = new XmlSerializer(typeof (Values));
using (
TextReader textReader = new StreamReader(
"E:\\hello.xml")
)
{
return
(Values) serializer.Deserialize(textReader);
}
}
I would pass the Stream from the OpenFileDialog to LoadValues(...), and use that to construct your StreamReader:
public Values LoadValues(Stream stream)
{
XmlSerializer serializer = new XmlSerializer(typeof(Values));
using (TextReader textReader = new StreamReader(stream))
{
return (Values)serializer.Deserialize(textReader);
}
}
and
if ((myStream = sFile1.OpenFile()) != null)
{
using (myStream)
{
Values v = LoadValues(myStream);
...
}
}
You need to pass the Stream as a parameter to your function.
I am having a strange problem.I am using the Windows API Code Pack for Microsoft .NET Framework for displaying custom thumbnails for my custom file extensions.I have used the Microsoft.WindowsAPICodePack.ShellExtensions namespace methods as mentioned in the documentation and I can successfully show a thumbnail.But i am encountering a strange problem.While the shell thumbnail handler is registered, I cannot delete the file for which the thumbnail is shown.The file gets deleted alright for normal delete but while using Shift+del the file disappears form explorer without errors but will return when i refresh the folder.The file will stay there until I restart explorer.exe process or if I focus the window and wait for 60secs the file disappears on its own.The returned file doesn't allow to get deleted again giving an access denied error message.I tried using LockHunter and it shows that explorer.exe is locking the file.I am confused guys.What am I doing wrong ?. How can I fix this?.
I am using Windows 7 64 bit,visual studio 2010
My code is as follows
namespace CustomThumbnail
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CustomThumbnail.XYZThumbnailer")]
[Guid("439a0bd3-8a44-401d-931c-3021ad8b1ad6")]
[ThumbnailProvider("XYZThumbnailer", ".xyz", ThumbnailAdornment = ThumbnailAdornment.VideoSprockets)]
public class MyThumbnailProvider : ThumbnailProvider, IThumbnailFromStream
{
public Bitmap ConstructBitmap(System.IO.Stream stream, int sideSize)
{
try
{
LogMessageToFile("Hello Stream");
XyzFileDefinition file = new XyzFileDefinition(stream);
using (MemoryStream mstream = new MemoryStream(Convert.FromBase64String(file.EncodedImage)))
{
LogMessageToFile("using Stream");
Bitmap bmp = new Bitmap(mstream);
LogMessageToFile(bmp.ToString());
return bmp;
}
}
catch (Exception ex)
{
LogMessageToFile(ex.ToString());
throw;
}
}
public void LogMessageToFile(string msg)
{
System.IO.StreamWriter sw = System.IO.File.AppendText(#"D:\test\testdoc.txt");
try
{
string logLine = System.String.Format(
"{0:G}: {1}.", System.DateTime.Now, msg);
sw.WriteLine(logLine);
}
finally
{
sw.Close();
}
}
}
}
New Code
public Bitmap ConstructBitmap(Stream stream, int sideSize)
{
try
{
Assembly assembly = Assembly.LoadFile(#"C:\Users\xxxx\Documents\Visual Studio 2010\Projects\MyThumbnailTest\Bin\Data\Data.dll");
Type type = assembly.GetType("Data.ThumbnailData");
MethodInfo foo = type.GetMethod("GetThumbnail");
var c= foo.Invoke(Activator.CreateInstance(type), new object[] { stream });
return (Bitmap)c;
}
catch (Exception ex)
{
LogMessageToFile("error "+ex.Message.ToString());
throw ex;
}
finally
{
stream.Close();
stream.Dispose();
}
}
And my GetThumbnail Method goes like this
public class ThumbnailData
{
public Bitmap GetThumbnail(Stream stream)
{
using (ZipFile zip = ZipFile.Read(stream))
{
ZipEntry image = zip.Entries.Where(p => p.FileName.ToLower().IndexOf(".png") > 0).FirstOrDefault();
if (image != null)
{
using (MemoryStream ms = new MemoryStream())
{
image.Extract(ms);
Bitmap bmp = new Bitmap(ms);
return bmp;
}
}
return new Bitmap(150, 150);
}
}
}
Without seeing the code, this is all I can think of:
Is your custom thumbnail code not closing the Stream for the file after it produces the thumbnail?
As my comment suggested try this:
public Bitmap ConstructBitmap(System.IO.Stream stream, int sideSize)
{
try
{
LogMessageToFile("Hello Stream");
XyzFileDefinition file = new XyzFileDefinition(stream);
using (MemoryStream mstream = new MemoryStream(Convert.FromBase64String(file.EncodedImage)))
{
LogMessageToFile("using Stream");
Bitmap bmp = new Bitmap(mstream);
LogMessageToFile(bmp.ToString());
return bmp;
}
}
catch (Exception ex)
{
LogMessageToFile(ex.ToString());
throw;
}
finally
{
stream.Close();
}
}