Import PDF in my xamarin shared code application - android

I want to import pdf in my xamarin android project.
I want to have a pdf in my application folder at the installation of the application.
Bu when i try to add this pdf in android ressources and try to open it, i get a path error.
i save my pdf in draxable ressource and call path like : #drawable/pdfname or pdfname. the two solution don t work.
Best regards,

ok, i put it in the asset folder. I want to send email with attachement. I try this but the app said me the file is empty. var file = new Java.IO.File("Assets/CV_2017.pdf"); var uri = Android.Net.Uri.FromFile(file); i set my ressource as "build as android asset".
To get the url of the file in assets folder, you can code for example like this:
var file = new File(Android.Net.Uri.Parse("file:///android_asset/CV_2017.pdf").ToString());
if (file.Exists())
{
var uri = file.AbsolutePath;
}
If you want to access the stream of the file under assets, you can use AssetManager and code for example like this:
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("CV_2017.pdf")))
{
//TODO:
}

Related

Open PDF file in external app with write permissions

I'm using Launcher.Default.OpenAsync(OpenFileRequest request) to open a PDF file in an external PDF editor. The file loads correctly, but to edit the document this external app asks you to make a copy and edit over that copy, not the original file. I can edit the original file if I open the PDF using the device (Galaxy Tab S6 Lite) file explorer but it's not possible to do the same if I open the same file from my MAUI app.
I see OpenFileRequest constructor asks for a ReadOnlyFile. Is there a way I could create an "OpenFileRequest" with write permissions, or an alternative way to launch the document editor with the file so I can edit it without having to create a copy?
Example code:
var filename = "example.pdf";
var file = new ReadOnlyFile(filename);
var openFileRequest = new OpenFileRequest("PDF Document", file);
await Launcher.Default.OpenAsync(openFileRequest);
I have tried to edit the pdf file with the Pdf Editor in some other apps. But all of them need to save as another file. So it seems only the file explorer can edit the original file.
In addition, the ReadOnlyFile is inherited from the FileBase. So you can try:
var filename = "example.pdf";
var file = new ReadOnlyFile(filename);
var openFileRequest = new OpenFileRequest("PDF Document", file as FileBase);
await Launcher.Default.OpenAsync(openFileRequest);
Actually, the other app doesn't have the permission to write the file in your app. You can refer to this case which is about editing pdf with external application does not overwrite existing file.
I can't find any native android api about grant the others app the write permission of the existing file. This should be the android permission limit.

Reading and existing file in Xamarin withC#

This code works:
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "File.text");
File.ReadAllText(file);
Pretty straight-forward, but when I display the variable "file" using a Pizel 5 simulator I get a weird path: /data/user/0/com.companyname.aikidohours/files/.local/share/File.text. I can write new data to the file and read from it. but I now want to read from an existing file and I can't figure out where to put the file. Can someone tell where to put a text file full of basic information that I need to read from in Xamarin for adnroid?
Thanks
Todd
If you want to deploy that file with your application, you can add it to the 'Assets' folder inside your_project_name.Android project.
WARNING!!! File put in this folder are READONLY!
In case this is ok for you,this is the code to access the file:
using Xamarin.Essentials;
using (var reader = new StreamReader(await FileSystem.OpenAppPackageFileAsync("file_path")))
{
string content = reader.ReadToEnd();
}

Finding a saved file from my app in the Android file system

I've created a new file using XmlSerializer and StreamWriter to persist data. From the test I did with my app, storing and restoring data using this method is working. Just for curiosity, I've tried to find the file created in the Android File System, without success. I've tried to find it with an Android app (ES File Explorer) and a desktop app (on Mac, Android File Transfer). In both case, I was unable to find the created file.
XmlSerializer xmlSerializer = new XmlSerializer (typeof(GAData));
TextWriter writer = new StreamWriter (FilePath);
xmlSerializer.Serialize (writer, this);
writer.Close ();
Console.WriteLine ("Data saved");
Where FilePath is define here :
// Determining the path
var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
FilePath = Path.Combine (documentsPath, "AppData.txt");
Console.WriteLine (Path.GetFullPath (FilePath));
The last Console.WriteLine is logging : /data/data/com.domain.myapp/files/AppData.txt
The exact same code is working like a charm on iOS, and I can see the file in the File System using an app on my Mac. Why can I find it in the Android File System? Or, where is it saved if it's somewhere else?
/data/data/com.domain.myapp/files/AppData.txt
That is private internal memory. Private as only your app has access.
Your app can reach it using getFilesDir().
Other app like ES File Explorer have no access.
You should be using FilesDir from the ContextWrapper
Path.Combine(FilesDir.Path , "MyFile.text");
Example, copies a file that is in the assets folder to the local file system
if (!File.Exists(Path.Combine(FilesDir.Path , "myFile.Text")))
{
using (var asset = Assets.Open("TextToCopyTo.txt"))
{
using (var dest = File.Create(Path.Combine(FilesDir.Path , "MyFile.text")))
{
asset.CopyTo(dest);
}
}
}

Access PDF file in assets folder on android

I have seen many question about accessing files in assets folder but can't seem to get a solid answer. I'm working on a application that would extract a text from PDF file thus I'm using iText Library to do that but my problem here is the file pathing of PDF.
I have tried using assetManager and assetManager opens that file but i think iText needs the file path so it can open the file by itself, i am not sure but that is my theory.
PdfReader reader = new PdfReader(<String PDF-file>);
then How do I access the file under assets folder using iText? if it is not possible is there a way to do that?
Here is sample code to get asset directory for file and use it for pdfreader.
code:
File f = FileUtils.fileFromAsset(this, "filename.pdf");
PdfReader pdfReader = new PdfReader(f.getPath());
You can get an InputStream object like this
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
and then use this constructor
public PdfReader(InputStream is)
throws IOException
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#PdfReader(java.io.InputStream)
I have solved my problem my using this:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));

Accessing an app's project files on the Android platform

So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];

Categories

Resources