I have an APP that is only a container for a VUE PWA. In one page the user could upload some file. I implemented the following solution to give the user the option to select a file from Galery or take a new photo.
https://blog.verslu.is/xamarin/xamarin-forms-xamarin/building-a-hybrid-app-with-xamarin-forms/
The problem is the FileReader is not able to read the file when the user takes a new photo with the camera. if the user selects an existing file, the upload works well. This is the error:
FileReader:
message: "The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired."
name: "NotReadableError"
I noticed one difference: when the user selects a file from galery, the URI path that the APP sent to the WebView looks like the following:
{content://com.android.providers.media.documents/document/image%3A32681}
When the user takes a new photo:
file:///storage/emulated/0/Pictures/JBSTRIP/IMG_637273232207442660.jpg
Anyone had the same problem? I didn't found any solution from web.
Thanks a lot!
I solved the problem.
I changed the directory where the picture from camera is saved:
Instead get the path using:
File imageStorageDir = new File (Android.OS.Environment.GetExternalStoragePublicDirectory (
Android.OS.Environment.DirectoryPictures), "APP");
I used the APP's folder:
File imageStorageDir = new File (_context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures), "APP");
Related
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.
For the last 2 weeks I've been successfully converting a WPF application to a Xamarin.forms android app. Everything works as intended except for 1 thing. I've been searching the web for a few days for a solution but I can't find a working solution.
This is my current setup:
Xamarin.Froms android app
Target android version: Android 10.0
Minimum android version: Android 7.0
Xamarin.Essentials version: 1.6.0
READ_EXTERNAL_STORAGE permission
WRITE_EXTERNAL_STORAGE permission
My app should be able to open a selected file, using the Xamarin.Essientials FilePicker, Do some stuff and save the File again to the same path. The file selection is handled in the following code:
var pickresult = await FilePicker.PickAsync();
if (pickresult != null)
{
try
{
var result = await pickresult.OpenReadAsync();
var reader = new StreamReader(result);
var jsonstring = reader.ReadToEnd();
ConfigData = JsonConvert.DeserializeObject<ProjectData>(jsonstring);
PLC_Data.ProjectLoaded = true;
L_LoadedProject.Text = pickresult.FileName;
ProjectData.Filepath = pickresult.FullPath;
reader.Close();
result.Close();
//Load login page
B_Next.IsEnabled = true;
}
This is where the problem occurs. Let me clearify what happens step by step:
The FilePicker opens a File picking window.
I navigate to my preferred file in the "download" folder, could be any folder.
The file is read and converted to an object
The file path is saved
when i check the filepath i get the following string:
"/storage/emulated/0/Android/data/com.FAIS.motorconfigapp/cache/2203693cc04e0be7f4f024d5f9499e13/303bc84665374139b6303c03255e9018/config1.json"
So the FilePicker copies the "External" file to the App cache folder and returns the filepath from the Apps cache folder. When i check this folder there is indeed a copy of my selected file inside it. Reading from and writing to this file works as it should.
Now the actual question:
Why does the FilePicker copy my selected file to his cache folder and how can i prevent it?
The user should be able to select a file from anywere on the device and find the updated file in the same location.
I've checked all my settings and in my opinion everyting should be correct so i don't get why a copy is created. Does anyone have an idea?
If you need more settings, pictures, versions please ask.
Thanks in advance
I am an iOS developer assigned a task in Android, so bear with me, I'm a bit green in Android.
I am attempting to load a local html file that is stored in the device download directory in a folder called user_guide. I want the html file to load in the device's browser (not in a webview for reasons outside the scope of this post). I am using the following code to launch the browser:
String downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
String path = "file://" + downloadPath + "/user_guide/index.html"; // + R.raw.test;
Uri pathUrl = Uri.parse(path);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(pathUrl);
context.startActivity(browserIntent);
I obtained the value of path by setting a breakpoint and manually set it in Chrome on my device to verify that is does work and loads the proper file. However, when I try to run this code in the app, I get the following toast message:
Cannot display PDF (index.html is of invalid format)
I'm confused about this message since I am trying to load an html file, not a PDF. Can anyone help me out? THanks!
Try changing "browserIntent.setData(pathUrl)" to
browserIntent.setDataAndType(pathUrl, "text/html")
to explicitly specify that it's HTML.
I found this suggestion at https://stackoverflow.com/a/7009685/10300291.
This is my question.
First I have create my personal folder on create
File parentFolder = new File(MainApplication.getInstance().getExternalCacheDir().getAbsolutePath()
+ File.separator+"myfolder");
if (!parentFolder.exists()) {
parentFolder.mkdirs();
}
Second in my application,i can receive file such as png from another application,and the file that i received has been saved in /Android/data/packageName/cache/myfolder/hashcode/example.png. And i choose the gallery to open it from intent chooser.
When gallery is open and I can see the png file.I kill my application's process and uninstall it.
Finally I install my application again.The path /Android/data/packageName doesn't been created! And create function show that
MainApplication.getInstance().getExternalCacheDir();
returns null!
Give me some advise! Thank you.
Try using
new Context().getApplicationContext().getExternalCacheDir().getAbsolutePath()
I'm trying to retrieve the file that the user wants to share via, for example, the Photo Gallery App. So, when they tap on the share button, and select my application, I'm retrieving the File's Uri through the method Uri fileUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);. This works fine.
Then, I try to get the File whose Uri is the one I have. I do that this way:
File file = new File(fileUri.getPath());
But, afterwards, when I try to open it and read it, I get the java.io.FileNotFoundException ENOENT (No such file or directory). Furthermore, when I execute the file.exists() method, it returns false. So I don't know where the problem is.
Here is the code:
Uri fileUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
File file = new File(fileUri.getPath());
FileInputStream imageInFile = new FileInputStream(file);
What am I doing wrong?
EDIT:
I already have the <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> tag in my manifest.
I must also add that the uri of the file which I retrieve is something like: "/0/1/content:/media/external/images/media/9412/ACTUAL/1989334571". As you can see, the file doesn't have any extension at all, so that might be the problem. Nevertheless, I don't know how to solve it.
EDIT 2:
It seems as it is a problem that started since KitKat...