How can I download a file from my phone? MediaDevices device.DownloadFile() method throws a Not Implmented exception - mediadevices

I am trying to use the MediaDevices library to download files from my phone. I am able to see the files on my phone, but MediaDevice.DownloadFile() throws a NotImplemented exception. I've got version 1.8.0 of the library. Here is my program:
using System;
using System.Diagnostics;
using System.IO;
using MediaDevices;
namespace ReadPhoneFile
{
class Program
{
static MediaDevice thePhone;
static void Main(string[] args)
{
var devices = MediaDevice.GetDevices();
foreach (MediaDevice device in devices)
{
try
{
thePhone = device;
device.Connect();
DownloadFiles(device, "e:/pictures/Rob's Test Phone/Lightroom");
device.Disconnect();
}
catch
{
// If it can't be read, don't worry.
}
}
Console.WriteLine("Press any key.");
Console.ReadKey();
}
static void DownloadFiles(MediaDevice device, string pcFolder)
{
var lightroomDir = device.GetDirectoryInfo(#"Phone\Android\data\com.adobe.lrmobile\files\carouselDocuments\a6f0275ee0b94c90b3c05f093250d4ef\Originals");
// ProcessLightroomFolder(photoDir, pcFolder, false);
foreach (MediaDirectoryInfo subFolder in lightroomDir.EnumerateDirectories())
{
ProcessLightroomFolder(subFolder, pcFolder, true);
}
}
static void ProcessLightroomFolder(MediaDirectoryInfo folder, string pcFolder, bool createFolder)
{
Console.WriteLine($"Processing folder {folder.Name} into folder {pcFolder}");
string pcSubFolder = $"{pcFolder}/{folder.Name}";
if (createFolder)
{
Directory.CreateDirectory(pcSubFolder);
}
foreach (MediaFileInfo file in folder.EnumerateFiles())
{
ProcessLightroomFile(file, pcSubFolder);
}
foreach (MediaDirectoryInfo subFolder in folder.EnumerateDirectories())
{
ProcessLightroomFolder(subFolder, pcSubFolder, true);
}
}
static void ProcessLightroomFile(MediaFileInfo file, string pcFolder)
{
Console.WriteLine($"Processing Lightroom file {file.Name} into folder {pcFolder}");
DownloadFile(file, pcFolder);
}
static void DownloadFile(MediaFileInfo phoneFile, string destinationFolder)
{
try
{
MemoryStream memoryStream = new System.IO.MemoryStream();
thePhone.DownloadFile(phoneFile.FullName, "c:/misc/downloadedFile.dng");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

there is an issue raised on github to solve this problem (https://github.com/Bassman2/MediaDevices/issues/32). The problem is that you are building your app on top of .NET Core 3.1, and this lib works well on .NET Framework 4.* as they warn when you build your project:
Package 'MediaDevices 1.8.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project

Related

Getting installed printers in .Net Maui

I am trying a to get a list of installed printers in my net maui app targeted for windows.
using System.Drawing.Printing;
namespace MauiApp2;
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
la.Text = PrinterSettings.InstalledPrinters.Count.ToString();
}
}
I tried adding the system.drawing.common reference to the project.
<ItemGroup>
<Reference Include="System.Drawing.Common">
<HintPath>C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.13\System.Drawing.Common.dll</HintPath>
</Reference>
</ItemGroup>
And get a list of installed printers using printersettings.installedprinters.
My project is targeted for windows. Am I doing it wrong? Or is there some other way to do it?
Edit:
I updated the code to the following:
#if WINDOWS
public string GetPrinterNames()
{
try
{
x= PrinterSettings.InstalledPrinters.Count.ToString();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
}
return x;
}
#endif
Still it crashes and shows this code in app.g.i.cs file:
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
Edit: system.drawing.printing is not supported for .Net Maui with target .net 7. I got the solution to work for now with target .net 6

Unity app crashes when built for iOS with camera enabled

I have an app which uses zxing to scan qr codes in the app. However when I build the app with these scripts in the scene the app crashes on startup. I thought it was something in the Awake() or Start() but I've wrapped those methods in a try catch, and even then I'm not getting any errors, and it doesn't crash on android and in the editor.
I don't have access to a Mac, and am using Unity Cloud Build to build it.
I also don't know how to enable permissions, I thought I did when creating the .p12 file, but I've also found that there's an info.plist file that I have to request permissions with.
Prior research I found this Unity Question about adding items to the Xcode project but not only did including the xcodeapi give me errors, but the using statements didn't work.
There are two scripts
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class WebCamController : MonoBehaviour {
public int desiredWidth = 1280;
public int desiredHeight = 720;
public int desiredFPS = 60;
public RawImage output;
[HideInInspector]
public WebCamTexture webcamTexture;
void Start ()
{
webcamTexture = new WebCamTexture(desiredWidth, desiredHeight, desiredFPS);
output.texture = webcamTexture;
Play();
}
public void Play()
{
webcamTexture.Play();
}
public void Pause()
{
webcamTexture.Stop();
}
}
and
using UnityEngine;
using System.Collections;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
using System;
public class CodeScanner : MonoBehaviour {
private static CodeScanner _instance;
public static CodeScanner Instance
{
get
{
if(null == _instance)
{
Debug.Log("Code Scanner Instance not found");
}
return _instance;
}
}
[Header("References")]
public WebCamController wcc;
[Header("Properties")]
private BarcodeReader codeScanner;
private string lastScanned = "";
public delegate void Found(string text, string type);
public event Found OnCodeScanned;
private bool active;
public void Awake()
{
_instance = this;
}
void Start () {
codeScanner = new BarcodeReader();
StartCoroutine(ReadCode());
wcc.Play();
}
IEnumerator ReadCode()
{
while (active)
{
try
{
var data = codeScanner.Decode(wcc.webcamTexture.GetPixels32(), wcc.webcamTexture.width, wcc.webcamTexture.height);
if (data != null)
{
//if (data.Text != lastScanned)
//{
OnCodeScanned(data.Text, data.BarcodeFormat.ToString());
//}
lastScanned = data.Text;
}
}
catch(Exception e)
{
}
yield return new WaitForSeconds(1.0f);
}
}
public void Activate()
{
wcc.Play();
active = true;
StartCoroutine(ReadCode());
}
public void Stop()
{
active = false;
wcc.Pause();
}
}
My device is added properly to the .p12 certificate I can compile and run the program without these scripts in the scene.

Fail to use IHostingEnvironment to get path

I'm creating project api in .Net Core 2.1, in my startup.cs, I added:
services.AddSingleton<IPathProvider, PathProvider>();
After that, reate IPathProvider interface and class:
public interface IPathProvider
{
string MapPath(string path);
}
public class PathProvider : IPathProvider
{
private IHostingEnvironment _hostingEnvironment;
public PathProvider(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public string MapPath(string path)
{
var filePath = Path.Combine(_hostingEnvironment.WebRootPath, path);
return filePath;
}
}
And then, in my api cs file, I write code:
private IHostingEnvironment _hostingEnvironment;
public PowerControlController(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public string MapPath(string path)
{
var filePath = Path.Combine(_hostingEnvironment.WebRootPath, path);
return filePath;
}
Now, in main api, I call mappath:
public ActionResult<string> GetListPowerSwitch()
{
try
{
var path = MapPath("../DataDen/DataDen.xml");
return path;
}
catch (Exception ex)
{
return ex.ToString();
}
}
It's work well when debug in local. But, when I publish it to IIS Web Server as new application, it's return ex.ToString() that:
System.ArgumentNullException: Value cannot be null.
Parameter name: path1
at System.IO.Path.Combine(String path1, String path2)
at LedControlPowerApi.Controllers.PowerControlController.GetListPowerSwitch() in E:\PROJECT EMEC\LedControlPrj\LedControlPowerApi\Controllers\PowerControlController.cs:line 55
Line 55 is: var path = MapPath("../DataDen/DataDen.xml");
Anyone tell me how to fix this bug ? Thanks
I think you need to use ContentRoot instead of WebRoot because for ASP.NET Core 2 API project there is no wwwroot folder in published API project.
Check this https://github.com/aspnet/Mvc/issues/6688

About good practice for GLib.Application and Soup.Server

I'm trying to create a simple server in vala using libsoup.
I am wondering if it is a good way to start a Soup.Server from a GLib.Application. Since using it synchronously (run is deprecated) is not recommended, the only way I found to keep it alive is to hold the default application.
public class Simple.Server : Soup.Server
{
public Server () {
Application.get_default ().hold ();
add_handler(null, null_handler);
}
private void null_handler (Soup.Server server, Soup.Message message,
string path, HashTable<string,string>? query,
Soup.ClientContext client) {
GLib.message ("path: %s", path);
message.status_code = 404;
message.set_response ("text/plain", Soup.MemoryUse.COPY, "".data);
}
}
public class Simple.App : Application
{
private Simple.Server server;
App () {
Object (application_id: "org.dev.simple-server",
flags: ApplicationFlags.FLAGS_NONE);
}
protected override void activate () {
base.activate ();
server = new Simple.Server();
try {
server.listen_all(8080, 0);
}
catch (Error e) {
GLib.message ("Error n°%u: %s", e.code, e.message);
}
}
protected override void shutdown () {
base.shutdown ();
server.disconnect ();
}
static int main (string[] args) {
App app = new Simple.App();
return app.run (args);
}
}
This is mimic of my code.
So here is the question, is it a good practice for starting the server, still using GLib.Application, or should I use (like examples say) only the server, starting/stopping manually the MainLoop ?
thanks.

PowerDesigner addin develop

Anyone knows how to develop an add-in for PowerDesigner? I was reading the document of PowerDesigner about how to create an ActiveX Add-in, it says "The ActiveX must implement a specific interface called IPDAddIn to become a PowerDesigner add-in.". But I don't know where the interface IPDAddIn is, and how to implement it ?
Here is the online document
I have this old example, which could give some ideas, even if not everything it up-to-date.
using PdAddInTypLib;
namespace MineSpace
{
[ComVisible(true)]
[Guid("A6FA0D26-77E8-4DD3-B27E-F4050C3D5188")]
public class Launcher : IPdAddIn {
// Main() manages the console or GUI interface
// the PdAddIn interface is managed by an instance of Launcher
[ComVisible(false)]
[STAThread]
public static void Main(String[] args) {
}
public Launcher() {
_app = null;
}
// IPdAddIn implementation
public void Initialize(Object anApplication) {
try {
_app = (PdCommon.Application)anApplication;
}
catch (Exception e) {
// process
}
}
public void Uninitialize() {
}
public String ProvideMenuItems(String aMenu, Object anObj) {
return "";
}
public int IsCommandSupported(String aMenu, Object anObj, String aCommand) {
return 0;
}
public void DoCommand(String aMenu, Object anObj, String aCommand) {
}
private PdCommon.Application _app;
}
}
with the corresponding part in the class declaration:
[HKEY_CLASSES_ROOT\MyPlugin.Launcher]
#="MyPlugin.Launcher"
[HKEY_CLASSES_ROOT\MyPlugin.Launcher\CLSID]
#="{13749EFC-1ADA-4451-8C47-FF0B545FF172}"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}]
#="MyPlugin.Launcher"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}\InprocServer32]
#="C:\windows\System32\mscoree.dll"
"ThreadingModel"="Both"
"Class"="MyPlugin.Launcher"
"Assembly"="MyPlugin, Version=1.0.1402.33688, Culture=neutral, PublicKeyToken=null"
"RuntimeVersion"="v1.0.3705"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}\ProgId]
#="MyPlugin.Launcher"
[HKEY_CLASSES_ROOT\CLSID\{13749EFC-1ADA-4451-8C47-FF0B545FF172}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}]
And the corresponding code to declare the add-in in PowerDesigner. If the File value is present, PowerDesigner could call DllRegisterServer on it, if the component is not yet registered.
[HKEY_LOCAL_MACHINE\SOFTWARE\Sybase\PowerDesigner 10\Addins\MyPlugin Launcher]
"Enable"="No"
"Class"="MyPlugin.Launcher"
"Type"="ActiveX"
"File"="d:\\myplugin\\myplugin.exe"

Resources