Android Studio - prompt to open a downloaded file with any application available - android-studio

I have an application which receives urls from a server, using Firebase. When the url is received, the file at that url is automatically saved on the local storage (they are gpx files).
But after this, I want my application to prompt the user to open the downloaded file with whatever navigation application they have installed on their phone. Something similar to when you go to the file manager yourself, click on a file, and the system shows you all applications which could open that file, and lets you chose (or opens it directly with your default setting, if you have one).
I have not been able to find anything about this, and searches containing "android studio" and "open file" show mostly tutorials about how to manage the source files themselves, so they are not helpful at all.

You could do something like:
String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1));
newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
private String fileExt(String url) {
if (url.indexOf("?") > -1) {
url = url.substring(0, url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf(".") + 1);
if (ext.indexOf("%") > -1) {
ext = ext.substring(0, ext.indexOf("%"));
}
if (ext.indexOf("/") > -1) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();
}
}
This allows you to get the type based on file extension. Then just startActivity passing in the newIntent. This should achieve what you are looking for.

Related

.NET Maui FilePicker results in This platform does not support this file type error for MacOS

I am building a desktop app in .NET Maui which targets Windows and MacOS.
When using the built-in FilePicker on Mac (Catalyst) I get the following error:
{System.PlatformNotSupportedException: This platform does not support this file type. at Microsoft.Maui.Storage.FilePickerFileType.GetPlatformFileType(DevicePlatform platform) at Microsoft.Maui.Storage.FilePickerFileType.get_Value() at Microsoft.Maui.S…}
I have tried multiple file types, including csv, jpg, text/csv but nothing works.
public async Task ImportCSV()
{
try
{
var customFileType = new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.WinUI, new[] { ".csv" } },
{ DevicePlatform.macOS, new[] {"csv"} }
});
PickOptions options = new()
{
PickerTitle = "Please select a csv file",
FileTypes = customFileType
};
var file = await FilePicker.Default.PickAsync(options);
if (file != null)
{
string filePath = AppStateService.fixMacFilePath(file.FullPath);
AppStateService.AppState.csvFilePath = filePath;
await InitiateCsvMapping();
}
}
catch (Exception ex)
{
}
}
I am running Visual Studio Community 2022 for Mac Preview
Version 17.4 Preview (17.4 build 715)
The documentation does say that I should "Enable iCloud capabilities" but does not provide any links or other information regarding this. I did some digging, and found some documentation that says that it necessary to do this via the Entitlements.plist file (but there is none in the Maui project) and seems to only be relevant when publishing your app to the App store which I am not during development. I don't even care about iCloud right now. I just want to select a CSV file from my own desktop. Is this the problem? Thanks!!
Here is the article
I don't think it has something to do with ICloud. It is just the wrong file type. File types are different on MacOS.They are called UTypes.
I haven't tried but it is likely:
public.comma-separated-values-text
More Info here: commaSeparatedText
And maybe it is DevicePlatform.MacCatalyst instead of macOs.

Webview issue, pdf links do not open, please assist

Im trying open pdf`s from a selectable list with a wevbview but when I click in app nothing happens, I have given the app internet permision also read and write(tough not needed) to ext storage. Can anyone give me a hint?
I have tested the google docs version it works with it but there are login and print buttons there where they dont work in webview, and Id like users to save the pdf they pick to view
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf")){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
try{
view.getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
//user does not have a pdf viewer installed
}
} else {
webview.loadUrl(url);
}
return true;
Ok so I changed the code so instead of viewing in the webview it should just download the file. I still have no idea why the above code was not calling the default pdf viewer on the device.I had installed over 5 pdf viewers on my device and made at least 3 of them default to test - didnt work. So in the end I just changed the shouldOverrideUrlLoading to download the file in the ext storage and from then, the user can open it by any app he wants/has installed. Solution:
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
return false;
}
also added permissions to manifest to access the default download manager and external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
this fixed it for me.

log4net web plugin location of log file

I have a windows forms application that runs in two different modes desktop mode and web plugin mode. I'm trying to put the log files using log4net in the same place. but when it is running as a web plugin my log file get put into the temporary internet folder of the users app data folder.
Code:
Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
if (Uri.TryCreate(uri, "log4net.config", out uri))
{
log4net.Config.XmlConfigurator.Configure(new FileInfo(uri.LocalPath));
}
_configured = true;
if (Utilities.WebPlugin)
{
var logNetHierarchy = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
foreach (var iAppender in logNetHierarchy.Root.Appenders)
{
if (iAppender is FileAppender)
{
var fileAppender = (FileAppender)iAppender;
fileAppender.File = #"C:\Users\" + Environment.UserName + #"\Company\Viewer\Web\log.xml";
fileAppender.ActivateOptions();
}
}
}
I would like to get them in the same place without including some kind of script.
stuartd was right soon as I put the site into trusted sites it worked perfectly.

Windows 10 App - File downloading

I am working with Windows 10 universal app and i want to download a file in that. The file link to Sharepoint server. I have passed token in headr to a web service and then service returned byte array to my WinJS.
Now i want to save the file, how can i do this? I tried several code samples but not working.
var folder = Windows.Storage.ApplicationData.current.localFolder;
folder.createFileAsync("document.docx", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
return Windows.Storage.FileIO.writeTextAsync(file, result.response);
}).then(function () {
//saved
});
I am using above code and it is creating new file but no content is placed there. Please suggest what to do.
You never open the file for WriteAccess. I have included code from my working app. First do this command
StorageFile ageFile = await local.CreateFileAsync("Age.txt", CreationCollisionOption.FailIfExists);
then do this:
// Get the file.
var ageFile = await local.OpenStreamForWriteAsync("Age.txt",CreationCollisionOption.OpenIfExists);
// Read the data.
using (StreamWriter streamWriter = new StreamWriter(ageFile))
{
streamWriter.WriteLine(cmbAgeGroup.SelectedIndex + ";" + DateTime.Now);
streamWriter.Flush();
}
ageFile.Dispose();

SSRS importer in C# 4.0, move reports from one server to another w/o changing format

yes, i know about File.Copy(...), but is there a web service method that can do the same thing? i am also worried about credentials needed to access the server. the inputs are to be the report filepath and the url to the server i want to move the report to WITHOUT CHANGING THE FORMAT. i have been looking at the web service ReportService2005 but not so sure it will work. other web services i have available are: ReportExecution2005, ReportingServices, ReportService, and ReportService2006. i would like to stay away from using rs.exe as well.
// Determine filename without extension (used as name in SSRS)
FileInfo fileInfo = new FileInfo(FileSystemPath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
try
{
// Determine filecontents
Byte[] fileContents = File.ReadAllBytes(fileInfo.FullName);
// Publish report
rsService.Warning[] warnings = this.rs.CreateReport(fileNameWithoutExtension, this.SSRSFolder, true, fileContents, null);
if (warnings != null)
{
foreach (rsService.Warning warning in warnings)
{
//Log warnings
}
}
}
catch
{
//handle error
}

Resources