image not displaying in flex mobile application using nativePath - android

I am creating an android application in flash builder 4 using 4.1 SDK on windows. The application first downloads some images from the internet and saves them in the desktopDirectory. Now I want to display downloaded images in my flex mobile application.
Issue:
Images are being downloaded from the internet successfully and saved in the desktopDirectory successfully.
Now when I try to display an image using nativePath then it is not displayed. A small blue icon with a questionmark is displayed instead of the image. The code that I am using is as below:
displayContainer.removeAllElements();
var image:spark.components.Image = new spark.components.Image();
var imageFile:File = File.desktopDirectory.resolvePath(desktopFilePath);
if(imageFile.exists)
{
var imagePath:String = File.desktopDirectory.resolvePath(desktopFilePath).nativePath;
image.source = imagePath;
trace("Image Path: " + imagePath);
displayContainer.addElementAt(image,1);
}
When I trace if the image file exists or not, it shows that the file is there. But the file is not displayed in the app.
However, the image is displayed when I hardcode the image path in the code as below:
<s:Image id="a1" source="data/02.jpg" />
Thus the image is there but the path is not being resolved programmatically.
What am I doing wrong? Please guide.
I am testing my application on an android tablet.

Instead of using file nativePath, I used file url and I got solution to my problem. Please see the code below which is working:
displayContainer.removeAllElements();
var image:spark.components.Image = new spark.components.Image();
var imageFile:File = File.desktopDirectory.resolvePath(desktopFilePath);
if(imageFile.exists)
{
var imagePath:String = File.desktopDirectory.resolvePath(desktopFilePath).url;
image.source = imagePath;
trace("Image Path: " + imagePath);
displayContainer.addElementAt(image,1);
}

Related

Xamarin.essentials FilePicker copies selected external file to cache folder

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

Android Doesn't Recognize Image Downloaded by Flutter App

Android Flutter app downloads an image from a server on another Android phone. After downloading, the file is unable to be opened or viewed by the phones gallery or file explorer. However, when examined by a hex editor, the downloaded file and the original file are exact copies, and when imported to windows, the "corrupt" downloaded file is view able by the Image Viewer. The only difference I could find between the files was the metadata examined by Windows. What could be the cause of this?
Original File on Android Server:
Downloaded File On Android Client:
Here's the code I'm using to create the file from a Uint8list:
Future<File> downloadFileAndroid(Uint8List fileBytes, String fileName) async{
var dir = await getExternalStorageDirectory();
File photoFile;
var photoDirectory = await Directory(dir.path + "/Downloader").create(recursive: true);
photoFile = await new File(photoDirectory.path + "/" + fileName).create();
if(await photoFile.exists()){
await photoFile.writeAsBytes(fileBytes);// also tried flush: true
print("Created file and it exists");
} else {
print("Error: tried to create file but it doesnt exist");
}
}
I faced this problem when downloading an image from the internet and setting a png extention to it. Using an extention on the file name usually causes this error. Consider using https://pub.dev/packages/image_downloader to download the image using the default function in the example code. With the default option, the extension is not determined by the coder but instead by itself.

Trouble loading local html file into default device browser in Android app

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.

Image File Saved on Android Device Not Opening

I need to display an image and some HTML associated with it in a WebView. I get the image as byte[]. I am creating a .PNG file using the following code.
private string CreatePNG(byte[] data)
{
string filePath = Android.OS.Environment.ExternalStorageDirectory.Path;
string fileName = System.Guid.NewGuid().ToString() + ".png";
File.WriteAllBytes(Path.Combine(filePath, fileName), data);
return Path.Combine(filePath, fileName);
}
Then I embed the .PNG file in my HTML using the following code.
html += #"<img src=""file://" + CreatePNG(page.DocumentImage) + "\"/><br><br>";
The image shows as blank in my app. The text shows properly. If I try to open the image directly on the phone after browsing from "File Manager", it still shows as blank. When I copy the image from the phone to my Mac, it opens up fine.
Can someone please advise what is going wrong here?

PDFTron Android Sample PDF

Using the Android sample in the WebViewer folder I have an application in Android Studio which when run on my device works and displays the xod file given as expected. However I've tried to change the lines:
String documentPath = getFilesDir().getPath() + "/GettingStarted.xod";
InputStream in = getAssets().open("xod/GettingStarted.xod");
to
String documentPath = getFilesDir().getPath() + "/sample.pdf";
InputStream in = getAssets().open("xod/sample.pdf");
And have also changed the endsWith(".xod") to endsWith(".pdf") but I only get a grey screen. For this to work does the file have to be a .xod? As it works for xod files but not pdf files.
Thanks for your time.
Yes, for mobile viewing you need to convert your files to the web-optimized XOD format.
The PDF backend for WebViewer is not available for mobile browsers. This is due to limitations of the hardware and the mobile browsers.
For desktop browsers, to switch from XOD backend to PDF backend, you need to follow the steps. See here for more details, especially if you run into any errors.
var myWebViewer = new PDFTron.WebViewer({
path: "lib",
type: "html5",
documentType: "pdf",
initialDoc: "GettingStarted.pdf"
}, viewerElement);
Notice the documentType parameter is set to pdf.

Categories

Resources