Unhandeled exception in application - c#

I have been working on a code that will grab the userName from the local machine and place it in a log file and start a .exe if the username has not been recoreded into the log file. I was able to run the code on my local machine, but when I put it on the server and run it I am given an error that reads:
"Unhandeled exception has occurred in your applications"
Details:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
***** Exception Text *******
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
at User.Form1.Form1_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
And here is my code////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace User
{
public partial class Form1 : Form
{
public const string dir = #"C:\Numara";
public const string path = dir + #"\Audit.log";
public const string TrackIT = #"\\tkahd-nti-1\TrackIt\Audit32.exe /Q";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//returns user name
//label1.Text = System.Environment.UserName.ToString();
string userName = System.Environment.UserName; //user name
if (!Directory.Exists(dir))
//directory does not exist
//create it
Directory.CreateDirectory(dir); //creates directory
//by this point directory is created
//now check file
if (!File.Exists(path))
//file does not exist, so create it
File.Create(path);
//Read data from the .dat file
string line = System.IO.File.ReadAllText(path);
//if the name of the logged in user
//is the same as the user name of the text file
//then exit program
if (line == userName)
Application.Exit();
else
//clear fields and write new name to file and begin audit
{
//clears fields
using (FileStream stream = new FileStream(#"C:\Numara\Audit.log", FileMode.Create))
{
using (TextWriter writer = new StreamWriter(stream))
{
//writer.Write("");
writer.Write(userName);
}
// writes new name to file
}
//StreamReader textIn =
// new StreamReader(
// new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
//begins audit
Process.Start(TrackIT);
Application.Exit();
}
}
}
}

The exception is being thrown because the process can't find the following network path:
\\tkahd-nti-1\TrackIt\Audit32.exe
The most likely cause is that the user account that the application is running under doesn't have access to that directory.
From your comments below it seems that the app is running using the permissions of the user who is logging on. You would need to grant anyone who might login read-only access to the "Audit32.exe" application.
However you don't need your own application to do this. If you open "Local Security Policy" from "administrative tools" (usually in control panel) you can open up the Local Policies --> User Rights Assignment folder from the tree view on the left and then change the "Log on locally" and "deny logon locally" settings to allow/deny login to individual users or groups of user. but be careful not to lock yourself out of the machine.

Try fully trusting the share...
http://blogs.msdn.com/b/shawnfa/archive/2004/12/30/344554.aspx?wa=wsignin1.0

Related

Access Denied when opening folder in UWP app

I have a simple UWP app that I want to open a folder to access all the files in with. I'm using the stock example code from Microsoft for how to make a folder picker in UWP. However, after picking the folder (any folder) and trying to access it I always get this exception:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'T:\temp' is denied.
Source=System.IO.FileSystem
StackTrace:
at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
at System.IO.Enumeration.FileSystemEnumerator`1..ctor(String directory, EnumerationOptions options)
at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options)
at System.IO.Enumeration.FileSystemEnumerableFactory.FileInfos(String directory, String expression, EnumerationOptions options)
at System.IO.DirectoryInfo.InternalEnumerateInfos(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
at System.IO.DirectoryInfo.EnumerateFiles()
at ShutterShock.MainPage.<Button_Click>d__1.MoveNext() in C:\Users\nixca\source\repos\ShutterShock\ShutterShock\MainPage.xaml.cs:line 37
Mainpage.xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace ShutterShock
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
string path = await GetOpenLocation();
var boop = new DirectoryInfo(path);
boop.EnumerateFiles();
}
async Task<string> GetOpenLocation()
{
string returnText;
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
// Application now has read/write access to all contents in the picked folder
// (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder);
returnText = folder.Path;
}
else
{
returnText = "Operation cancelled."; //todo make this an exception, catch that exception
}
return returnText;
}
}
}
I get the exception on the "boop.EnumerateFiles();" line.
So of course right after posting this it came to me, but I'll leave this up because I didn't actually find an answer anywhere before asking. The Microsoft example is dumb, and unless all you want is the path of the folder, returning the path is useless. The UWP filepicker doesn't actually grant you System.IO level access to the folder, all it gives you is a StorageFolder you can work on. If you want to do anything useful, you need to return the StorageFolder object, you can use that to actually work on files. This is probably obvious to people who actually know what they're doing.
If you wan to get all files in a selected folder, then you can use folder.GetFilesAsync().
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
var files = await folder.GetFilesAsync();
foreach(var file in files)
{
Debug.WriteLine(file.Path);
}
}
Here is the my result.

Xamarin Forms - File System

I'm developing a cross-platform application (UWP - Target version 16299, Android Target Version Pie and iOS; Visual Studio 2019 - Xamarin.Forms 4.1.0) which needs to communicate with a local DB file created inside AppData. All nice and fun until I try to export the information to another file, outside the AppData.
I've tried a bunch of things with no success and I'm curious why it works for you and not for me.
This is the latest code I've tested, similar to the others and with the same result:
Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll
using Plugin.FilePicker;
using Plugin.FilePicker.Abstractions;
using Plugin.Permissions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
namespace Testing
{
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private Plugin.FilePicker
.Abstractions.FileData file;
private async void ImportBtn_Clicked(object sender, EventArgs e)
{
try
{
file = await CrossFilePicker.Current.PickFile();
}
catch (Exception ex)
{
}
file.FileName = "rooms.jpg";
file.FilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
await CrossFilePicker.Current.SaveFile(file);
}
private void ExportBtn_Clicked(object sender, EventArgs e)
{
{
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string localFilename = "download.txt";
string localPath = Path.Combine(documentsPath, localFilename);
try
{
File.Create(localPath);
}
catch(Exception ex)
{ Debug.WriteLine(ex); }
Debug.WriteLine(localPath);
}
}
}
}
I want to mention that all capabilities have been ticked, in a desperate attempt to get something to work, the import_clicked works as intended and the folders tried (Personal, Music, Pictures, Commons) are empty.
After testing ExportBtn_Clicked code in local proejct . It occurs the same error.
Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll
System.UnauthorizedAccessException: Access to the path 'C:\Users\xxx\Documents\download.txt' is denied.
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.File.Create(String path)
at App8.MainPage..ctor()
This problem happens in UWP when accessing file from outside of app . Then I find a similar discussion about this error.
Access to the path 'C:\Sites\content\ServerIpAddress.txt' is denied
Then I refer to File access permissions ,the problem is UWP want to get files outside the scope of the app, you need to use Windows.Storage.If have a necessary to use Documents folder , you can refer to this to access it.
Accessing additional locations
using Windows.Storage;
var x = await StorageFolder.GetFolderFromPathAsync(#"C:\Users\UserName\Documents"); await x.CreateFileAsync("newfile.txt");
Note: Better using sandbox folder in application ,this will have the full permission .
In Android , if want to use External storage to realize it as follow:
Java.IO.File sdCard = Android.OS.Environment.ExternalStorageDirectory;
Java.IO.File dir = new Java.IO.File (sdCard.AbsolutePath + "/MyFolder");
dir.Mkdirs ();
Java.IO.File file = new Java.IO.File (dir,"download.txt");
if (!file.Exists ()) {
file.CreateNewFile ();
file.Mkdir ();
FileWriter writer = new FileWriter (file);
// Writes the content to the file
writer.Write (jsonData);
writer.Flush ();
writer.Close ();
}

C# .NET Missing Method Exception when opening ZipArchive created with System.IO.Compression

I have a C# WinForms .NET app in which I'm trying to write to a zip archive and read from it using System.IO.Compression.
Here's now I create the ziparchive:
public void SaveStdV20ZipProject(string strfilepath, clsProjectInfo GameInfo)
{
using (var ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
string strProjectData = String.Empty;
StringBuilder sb = new StringBuilder();
// First, we add the Game Info data...
sb.AppendLine(GameInfo.strGameVersion);
sb.AppendLine(GameInfo.strProjectType);
sb.AppendLine(GameInfo.strGameTitle);
sb.AppendLine(GameInfo.strAuthor);
sb.AppendLine(GameInfo.strCreationDate);
sb.AppendLine(GameInfo.blTSImagePresent.ToString());
sb.AppendLine(GameInfo.blTSAudioPresent.ToString());
sb.AppendLine(GameInfo.blTSVideoPresent.ToString());
sb.AppendLine(GameInfo.blFSSImagePresent.ToString());
sb.AppendLine(GameInfo.blFSSAudioPresent.ToString());
sb.AppendLine(GameInfo.blFSSVideoPresent.ToString());
sb.AppendLine(GameInfo.intTotalQuestions.ToString());
sb.AppendLine(GameInfo.intTotalMediaItems.ToString());
sb.AppendLine(GameInfo.intTotalCategories.ToString());
sb.AppendLine(GameInfo.blTiebreakerPresent.ToString());
// Next, create an archive entry for the Game Data string...
strProjectData = sb.ToString();
var ProjectData = archive.CreateEntry("ProjectData.txt");
using (var entryStream = ProjectData.Open())
using (var streamWriter = new StreamWriter(entryStream))
{
streamWriter.Write(strProjectData);
}
// We're done writing all the data for this project. Now let's write it to the file...
using (var fileStream = new FileStream(#strfilepath, FileMode.Create))
{
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fileStream);
}
}
}
}
And here's how I open it:
public void OpenStdV20ZipProject(string strfilepath)
{
string zipPath = strfilepath;
string extractPath = Path.GetTempFileName();
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
using (StreamReader sr = new StreamReader(extractPath))
{
clsProjInfo.strGameVersion = (string)sr.ReadLine();
clsProjInfo.strProjectType = (string)sr.ReadLine();
clsProjInfo.strGameTitle = (string)sr.ReadLine();
clsProjInfo.strAuthor = (string)sr.ReadLine();
clsProjInfo.strCreationDate = (string)sr.ReadLine();
clsProjInfo.blTSImagePresent = Convert.ToBoolean(sr.ReadLine());
clsProjInfo.blTSAudioPresent = Convert.ToBoolean(sr.ReadLine());
clsProjInfo.blTSVideoPresent = Convert.ToBoolean(sr.ReadLine());
clsProjInfo.blFSSImagePresent = Convert.ToBoolean(sr.ReadLine());
clsProjInfo.blFSSAudioPresent = Convert.ToBoolean(sr.ReadLine());
clsProjInfo.blFSSVideoPresent = Convert.ToBoolean(sr.ReadLine());
clsProjInfo.intTotalQuestions = Convert.ToInt32(sr.ReadLine());
clsProjInfo.intTotalMediaItems = Convert.ToInt32(sr.ReadLine());
clsProjInfo.intTotalCategories = Convert.ToInt32(sr.ReadLine());
clsProjInfo.blTiebreakerPresent = Convert.ToBoolean(sr.ReadLine());
}
}
}
}
} // <-THIS IS LINE 1320
It throws a Missing Method Exception and I've looked high and low in the Internet for a fix. Here's the stack trace:
System.MissingMethodException occurred
HResult=0x80131513
Message=Method not found: 'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'.
Source=TASv20ClsLib
StackTrace:
at TASv20ClsLib.clsOpenStandardProject.OpenStdV20ZipProject(String strfilepath) in C:\Users\Reuben\Documents\Visual Studio 2017\Projects\C# Projects\TRIVIA AUTHOR SUITE V20 PROJECTS 2\TAS v20 Zip Test Jun14 2\TASv20ClsLib\Class1.cs:line 1320
at Trivia_Author_v20.frmMain.openV20ProjectToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Reuben\Documents\Visual Studio 2017\Projects\C# Projects\TRIVIA AUTHOR SUITE V20 PROJECTS 2\TAS v20 Zip Test Jun14 2\Trivia Author v10 New Approach\frmMain.cs:line 1627
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Trivia_Author_v20.Program.Main(String[] args) in C:\Users\Reuben\Documents\Visual Studio 2017\Projects\C# Projects\TRIVIA AUTHOR SUITE V20 PROJECTS 2\TAS v20 Zip Test Jun14 2\Trivia Author v10 New Approach\Program.cs:line 126
The ZipFile.OpenRead(string) method was added only in .NET 4.5. It does not exist in previous versions.
Your question is not clear about which version of .NET your project targets, nor which version of .NET is installed where you are trying to run it, but undoubtedly, you have targeted .NET 4.5 or higher, but are trying to run the code on which only an older version of .NET is installed.
To fix this, either make sure .NET 4.5 is installed on the machine where you want to run the code, or use the older API. For example, you can write your own OpenRead(string) method without much difficulty:
ZipArchive OpenRead(string filename)
{
return new ZipArchive(File.OpenRead(filename), ZipArchiveMode.Read);
}
}
This is part of a whole host of binding issues introduced in 4.6x versions of .Net Framework. It may work somewhere, but not other places.
Most of this is related binding redirect issues that they have had between 4.6.1 and 4.7.1. Fixed in 4.7.2. These issues usually manifest themselves when working in the Framework and referencing .Net Standard packages
It is addressed in this framework issue: https://github.com/dotnet/corefx/issues/7702
Your best bet is to use binding redirects in your .Config file, or upgrade to .Net Framework 4.7.2 or later
Here i used Ionic and its working good, You can use
You need to import Ionic.zip to your project.
using (var zip = Ionic.Zip.ZipFile.Read("YourFilePAth"))
{
<enter code here>
};

DirectoryNotFoundException was unhandled by user code? (Visual Studio 2013, C#)

When I try to run this program, which writes details about a book to an xml file when a button is clicked, I get a DirectoryNotFoundException. How can I rewrite the address for my books.xml file under the App_Data folder?
Here are the details about the error.
An exception of type 'System.IO.DirectoryNotFoundException' occurred in System.Xml.dll but was not handled in user code
Additional information: Could not find a part of the path 'C:\App_Data\books.xml'.
Here is the Default.aspx code for reference:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace LibraryShelf
{
public partial class Default : System.Web.UI.Page
{
//protected void Page_Load(object sender, EventArgs e)
//{
//}
static void addNode(string fileName, XmlDocument xmlDoc)
{
XmlElement bookElement = xmlDoc.CreateElement("book");
bookElement.SetAttribute("name", "DotNet Made Easy");
XmlElement authorElement = xmlDoc.CreateElement("author");
authorElement.InnerText = "microsoft";
bookElement.AppendChild(authorElement);
XmlElement priceElement = xmlDoc.CreateElement("price");
priceElement.InnerText = "50";
bookElement.AppendChild(priceElement);
xmlDoc.DocumentElement.AppendChild(bookElement);
xmlDoc.Save(fileName);
}
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.Combine(Request.ApplicationPath, "App_Data/books.xml");
XmlTextReader _xmlTextReader = new XmlTextReader(fileName);
XmlDocument _xmlDocument = new XmlDocument();
_xmlDocument.Load(_xmlTextReader);
//Note: Close the reader object to release the xml file. Else while saving you will get an error that it is
//being used by another process.
_xmlTextReader.Close();
addNode(fileName, _xmlDocument);
}
}
}
The application folder is not the physical path to the web site, it's the path from the domain root to the application root. This is usually an empty string, unless you are using a virtual directory or an application subfolder on the server.
Use the MapPath method to get the physical path of a virtual address:
string fileName = Server.MapPath("~/App_Data/books.xml");
try this -
string path = System.IO.Path.GetFullPath(Server.MapPath("~/App_Data/books.xml"));
Server.MapPath will get you the location of the file.

Xamarin/Android - Issue reading locally stored photo from camera - no read access

I'm trying to create a simple test app to take photos in Android, using Xamarin. When I get this app working (or so I hope), i'll use the code in a real app that I'm working on. I'm using the following recipe from Xamarin as my basis:
http://docs.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/
The major difference is that I need to store images locally, and not on the SD card. I'm able to successfully take a picture (with the Android simulator). I can see the file in the file structure using ADB and can successfully copy and open the file on my PC. However, I'm unsuccessfull in accessing the file in the app, probably due to user rights.
Please note that I was successfull in creating my own .txt files, and reading them back using either System.IO and Java.IO.
Please review the following code. My app crashes when using "System.IO.File.ReadAllText" and gives me "Access to the path "/data/data/CameraAppDemo.CameraAppDemo/files/photo.jpg" is denied.". And whatever I try (absolute, relative paths, uri's), objBitmap is always null.
ADB says that "photo.jpg" has -rwxrwx--- rights, and though I'm not entirely sure, I think that should be more than sufficient
On the other hand, maybe the intent still has a lock on "photo.jpg"? Or something else is going on...
And one final note, I'm using System.IO.File.ReadAllText just for testing purposes. I experimented with stream readers as well, but with the same result. Also, though I believe this step is unnecessary, I enabled "WriteExternalStore" in the Manifest
namespace CameraAppDemo
{
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Provider;
using Android.Widget;
using Java.IO;
using Environment = Android.OS.Environment;
using Uri = Android.Net.Uri;
[Activity(Label = "Camera App Demo", MainLauncher = true)]
public class MainActivity : Activity
{
private File _file;
private string _basePath;
private ImageView _imageView;
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
var objBitmap = BitmapFactory.DecodeFile(_file.AbsolutePath) ;
Console.WriteLine ("objBitmap = null : " + (objBitmap == null).ToString ());
var strOutput = System.IO.File.ReadAllText (FileManager.BasePath + "/photo.jpg");
Console.WriteLine (strOutput);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
FileManager.SetupFolderStructure();
if (IsThereAnAppToTakePictures())
{
Button button = FindViewById<Button>(Resource.Id.myButton);
_imageView = FindViewById<ImageView>(Resource.Id.imageView1);
button.Click += TakeAPicture;
}
}
private bool IsThereAnAppToTakePictures()
{
Intent intent = new Intent(MediaStore.ActionImageCapture);
IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
return availableActivities != null && availableActivities.Count > 0;
}
private void TakeAPicture(object sender, EventArgs eventArgs)
{
System.IO.Directory.Delete (FileManager.BasePath, true);
_basePath = FileManager.BasePath;
_file = new Java.IO.File (_basePath, "photo.jpg");
Intent intent = new Intent(MediaStore.ActionImageCapture);
intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
StartActivityForResult(intent, 0);
}
}
}
//Part of the FileManager class:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Android.Graphics;
namespace CameraAppDemo
{
public class FileManager
{
public static string BasePath {
get {
var libraryPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
if (Directory.Exists (libraryPath) == false) {
Directory.CreateDirectory (libraryPath);
}
return libraryPath;
}
}
}
}
==== Edit ====
It seems that I'm simply not able to read the file. As an ex-webdeveloper, I'm fairly new to programming for mobile, let alone the combo of C# and Java and I'm still learning a lot.
Anyway, I added the following lines:
Console.WriteLine("Setting file :" + _file.SetReadable (true));
Console.WriteLine("Can read :" + _file.CanRead());
Both cases return False. I can't read the file, and I am unable to give read access.
So, any ideas? Is this by design? Can I tell the Intent for taking images to give me read access, or is there another workaround?
If everything fails, I'm hoping to workaround the problem by saving to the SD card first and then copying the file to the local filesystem. But that's something I rather would not do; I can't guarantee that the end users have an SD card, and the pictures should not be deleted by accident.

Categories

Resources