Xamarin.Forms Open local PDF - pdf

I tried to open a locally stored pdf with xamarin.
example code:
var files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
var filepath = "file://" + files[0];
if (File.Exists(filepath))
{
await Launcher.OpenAsync(filepath);
}
But the file does not open. The only message I get is (android device):
what do I miss?
EDIT
the variable filepath contains:
file:///data/user/0/com.companyname.scgapp_pdfhandler/files/.config/test.pdf
also tried
file://data/user/0/com.companyname.scgapp_pdfhandler/files/.config/test.pdf
does not help

Figured I would add my comment as an answer for easier visibility in case others run into it in the future.
Pass a OpenFileRequest object instead, if you use a string it has to be the correct uri scheme for it. I suspect the uri scheme you are passing to it isn't something that is understood by the system

Related

With Kotlin write to SD-card on Android

I have read a lot of questions and answers, but are not really satisfied and successful
My Problem: write with Kotlin to a sdcard to a specific directory
Working is
var filenamex = "export.csv"
var patt = getExternalFilesDirs(null)
var path = patt[1]
//create fileOut object
var fileOut = File(path, filenamex)
//create a new file
fileOut.createNewFile()
with getExternalFIlesDirs() I get the external storage and the sdcard. With path = patt[1] i get the adress of my sd-card.
this is
"/storage/105E-XXXX/Android/data/com.example.myApp/files"
This works to write data in this directory.
But I would like to write into an other directory, for example
"/sdcard/myApp"
A lot of examples say, this should work, bit it does not.
So I tried to take
"/storage/105E-XXXX/myApp"
Why doesn't it work? Ist the same beginning of storage /storage/105E-XXXX/, so it is MY sd-card.?
As I mentioned, it works on the sd-card, so it is not a problem of write-permission to the sdcard?
Any idea?
(I also failed with FileOutputStream and other things)
Try Out this..
var filenamex = "export.csv"
var path = getExternalStorageDirectory() + "//your desired folder"
var fileOut = File(path, filenamex)
fileOut.createNewFile()

Kotlin - get Firebase Image DownloadUrl returns "com.google.android.gms.tasks.zzu"

I am trying to upload images to Real time Firebase Database by creating two folders using Compressor library and need to display image like messenger with username but i am unable to display image due to url issue
var filePath = mStorageRef!!.child("chat_profile_images")
.child(userId + ".jpg")
//Create another directory for thumbimages ( smaller, compressed images)
var thumbFilePath = mStorageRef!!.child("chat_profile_images")
.child("thumbs")
.child(userId + ".jpg")
filePath.putFile(resultUri)
.addOnCompleteListener{
task: Task<UploadTask.TaskSnapshot> ->
if (task.isSuccessful) {
//Let's get the pic url
var donwloadUrl = task.result?.storage?.downloadUrl.toString()
Log.d(TAG, "Profilepic link: $donwloadUrl")
//Upload Task
var uploadTask: UploadTask = thumbFilePath
.putBytes(thumbByteArray)
uploadTask.addOnCompleteListener{
task: Task<UploadTask.TaskSnapshot> ->
var thumbUrl = task.getResult()?.storage?.downloadUrl.toString()
Log.d(TAG, "Profilepic link: $thumbUrl")
i tried to change downloadUrl
filepath.downloadUrl.toString
thumbFilePath.downloadUrl.toString
but both these values getting "com.google.android.gms.tasks.zzu"
i also tried to change
task.result.sessionurl.downloadUrl.toString
for this one i am getting downloadUrl but not a complete solution for my problem as still i cannot display image i need to get thumbUrl downloadUrl
You have the exact same and very common misunderstanding as in this question, except it's in java. You should follow the documentation here to understand get getDownloadUrl works. As you can see from the linked API documentation, it's not a property getter, it's actually a method that returns a Task<Uri> that tracks the asynchronous fetch of the URL you want, just like the upload task:
filePath.downloadUrl
.addOnSuccessListener { urlTask ->
// download URL is available here
val url = urlTask.result.toString()
}.addOnFailureListener { e ->
// Handle any errors
}
This will only work after the upload is fully complete.
Correct way of getting download link after uploading
here
Just putting out there, I also encounter the same problem,
but both these values getting "com.google.android.gms.tasks.zzu"
but it wasn't the same mistake from the OP
used addOnCompleteListener instead of addOnSuccesslistener
My Error code:
imageRef.downloadUrl.addOnCompleteListener { url ->
val imageURL = url.toString()
println("imageURL: $imageURL , url: $url")
addUserToDatabase(imageURL)
}

Do I need to call CachedFileManager.DeferUpdates in Windows 8 app

In the file picker Windows 8 sample a file is saved like this:
CachedFileManager.DeferUpdates(file);
await FileIO.WriteTextAsync(file, stringContent);
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
I'm serialising an object as XML so doing it slightly differently:
// CachedFileManager.DeferUpdates(file);
var ras = await file.OpenAsync(FileAccessMode.ReadWrite);
var outStream = ras.GetOutputStreamAt(0);
var serializer = new XMLSerializer();
serializer.Write(myObject, outStream);
// FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
It works with or without the CachedFileManager (commented out above).
So, should I include the CachedFileManager and if I do use it am I saving the file in the right way.
This code works and saves the file fine, but I don't like including code that I don't understand.
Yes, this code will work without CachedFileManager. But, when you use CachedFileManager, you inform the file provider that the file is in process of change. If your file is located on SkyDrive it is faster to create a file and upload it at once instead of update it multiple times.
You can have the full story there : http://www.jonathanantoine.com/2013/03/25/win8-the-cached-file-updater-contract-or-how-to-make-more-useful-the-file-save-picker-contract/
It simply tells the "repository" app to upload the file.

windows 8 modern ui apps - access to data

Where can i find folder with installed modern ui apps? Im developing some app which uses .txt files to store information (win8 doesnot support datebase on arm - facepalm) but they seem to not work properly - thats why i want to access them.
Thanks!
That is not the correct way of doing things in Metro. I assume you mean db files, or txt files. Simply access the local text file from the project folder.
Here is a great tutorial on how you would go about doing so: http://www.codeproject.com/Articles/432876/Windows-8-The-Right-Way-to-Read-Write-Files-in-Win
An example:
private async void ProjectFile()
{
// settings
var _Path = #"Metro.Helpers.Tests\MyFolder\MyFolder.txt";
var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// acquire file
var _File = await _Folder.GetFileAsync(_Path);
Assert.IsNotNull(_File, "Acquire file");
// read content
var _ReadThis = await Windows.Storage.FileIO.ReadTextAsync(_File);
Assert.AreEqual("Hello world!", _ReadThis, "Contents correct");
}

Validate FileUpload Control In ASP.Net

I want to validate asp FileUpload Control using in asp.net. That should accept only the .xlsx and .xls files. Validations may be on server side or client side. but client side will better.
How to do this?
yes.. you can validate by simple client script..
var uploadfile=document.getElementById("<%=FileUpload1.ClientID%>").value;
//get substring to find out extension
var ext= uploadfile.extension //get substring to find out extension
if (ext !=".xls")
{
alert("invalid format.")
return false;
}
On server side u can do it like this
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() != ".xlsx" && fileExtension.ToLower() != ".xls")
{
Labelupload.Text = "Only Files with ..xlsx and .xls extension are allowed";
Labelupload.ForeColor = System.Drawing.Color.Red;
}
Checking for a true file type is not simple. A user can spoof the file type (e.g., .exe renamed .pdf), and a malicious file will then appear as a benign one. And checking the MIME type server-side won't fix this; i.e., an .exe will show a MIME of "application/pdf" if renamed as .pdf. An easy way to check the true file type using System.IO.BinaryReader is described here using System.IO.BinaryReader:
http://forums.asp.net/post/2680667.aspx
and VB version here:
http://forums.asp.net/post/2681036.aspx
Note that you'll need to know the binary 'codes' for the file type(s) you're checking for, but you can get them by implementing this solution and debugging the code.