Get real path from URI? Android 9+ - java

I have the uri of my files. Now I need to get its real path to send them. (the uri is like: "content://some-path").
This is for Android 9

So with Android 9+ you can only use file paths inside you applications private storage area otherwise you have to use URI's and ContentResolvers
See https://developer.android.com/training/data-storage/files/external-scoped for details of the changes in 9+
So I see three solutions when using Retrofit2
Get a Java FileDescriptor from a contentResolver and read the file from the contentResolver, writing it to your App's private storage area. You can then get a Java File Object as normal from this copy of the file. This is similar to stackoverflow.com/a/52814247/3518278 as suggested by ViVekH
Get a Java FileDescriptor from a contentResolver and read it in to a in memory Byte array. I'm not a Retrofit2 user but I believe you can create Request Body or Multipart part from a Byte array instead of a Java File object.
Raise a feature request with RetroFit2 / OKHTTP to be able to give it a Java FileDescriptor object instead of a File Object
Note with the contentResolver you can query it to get the "Display Name" as the filename part of the Path.

Related

Storage Access Framework - Saving Uri

My app needs to archive a folder Uri (from an Inten with ACTION_OPEN_DOCUMENT_TREE and the right flags inside that has been returned to the app from user selection) in the suitable form to use it next time the app is run or the device is booted (takePersistableUriPermission is used).
Saving the Uri path as a string seems not to be enough, because
Uri uri;
uri=Uri.parse(uriPath);
docUri= DocumentsContract.buildDocumentUriUsingTree(uri,
DocumentsContract.getTreeDocumentId(uri));
gives an error like invalid Uri.
Saving also the Uri id is not useful.
I see that the Uri has many parameters inside. I checked which parameters have values inside my sample Uri and a few are.
So I would know how to save a Uri so the app has all necessary data to recreate it and the DocumentsContract class (or similar) can query its parameters not throwing any exceptions.
Any suggestion is welcome
The right way to archive a Uri is by means of saving the uri.toString() value.
Then to retrieve it: uri=Uri.parse(archivedUriString);

Getting a .png image by Apache HttpClient on Android

I'm trying to get a .png file to render in a WebView after getting the file through an apache HttpClient get request. I'm actually reading the HttpResponse contents into a simple byte array -> (evaluating the contents into a String when debugging).
It appears if I convert those bytes into a string to compare with the actual file, they look different. Is there a specific way to retrieve .png file
using the apache HttpClient?
example compare, my test is on the left and actual file on right

SDMX-ML: SAS libname XML

Eurostat data can be downloaded via a REST API. The response format of the API is a XML file formatted according to the SDMX-ML standard. With SAS, very conveniently, one can access XML files with the libname statement and the XML or XMLv2 engine.
Currently, I am using the xmlv2 engine together with the automap= option to generate an xmlmap to access the data. It works. But the resulting SAS data sets are very unstructured, and for another data set to be downloaded the data structure might change. Also the request might depend on the DSD-file that Eurostat provides for each database item within a different XML file.
Here comes the code:
%let path = /your/working/directory/;
filename map "&path.map.txt";
filename resp "&path.resp.txt";
proc http
URL="http://ec.europa.eu/eurostat/SDMX/diss-web/rest/data/cdh_e_fos/..PC.FOS1.BE/?startperiod=2005&endPeriod=2011"
METHOD="GET"
OUT=resp;
run;quit;
libname resp XMLv2 automap=REPLACE xmlmap=map;
proc datasets;
copy out=WORK in=resp;
run;quit;
With the code above, you can view all downloaded data in your WORK library. Its a mess.
To download another time series change parameters of the URL according to Eurostat's description.
So here is my question
Is there a way to easily generate a xmlmap from a call to the DSD file so that the data are stored in a well structured way?
As the SDMX-ML standard is widely used in public institutions such as the ECB, Eurostat, OECD... I am wondering if somebody has implemented requests to the databases, already. I know about the tool from Banca Italia which uses a javaObject. However, I was wondering if there might be a solution without the javaObject.

How to Retrieve the Download Link for Large Video Files

I would like to retrieve the download link for large video files. I have no problems with small video files but with large videos, the response from the server is that the file
"exceeds the maximum file size that Google can scan"
I want to use the link as the source to a video tag. But because that link gives me the error, I can't use it.
I'm using the Java SDK and I'm using File.getWebContentLink() to get the link. I've tried getDownloadLink() but that one doesn't even work.
Basically, is there anyway I can get the download link for large video files?
getWebContentLink() is designed for interactive users (browsers).
Instead, at the raw API level you'll want to use File.get with alt=Media AND also set the acknowledgeAbuse flag if you initially get returned the 'Google can't scan'. Read more on downloading files here and the abuse flag here.
In the Java client library, it'd look something like this:
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.set("acknowledgeAbuse", true)
.executeMediaAndDownloadTo(outputStream);
Disclaimer, I haven't compiled the above.
Note: Do not use the .../host/id method mentioned in the other answer - that method is deprecated and scheduled to stop serving content by end of August, 2016 (this year)
Try using https://googledrive.com/host/id where id is the file's ID. Inspired by http://www.scriptscoop2.com/t/1eb5579419c6/issue-when-trying-to-stream-a-video-from-google-drive-inside-html5-vid.html.

Capture generated output file path and name using CSSDK

We are in the process of converting over to using the XSLT compiler for page generation. I have a Xalan Java extention to exploit the CSSDK and capture some meta data we have stored in the Extended Attributes for output to the page. No problems in getting the EA's rendered to the output file.
The problem is that I don't know how to dynamically capture the file path and name of the output file.
So just as POC, I have the CSVPath hard coded to the output file in my Java extension. Here's a code sample:
CSSimpleFile sourceFile = (CSSimpleFile)client.getFile(new CSVPath("/some-path-to-the-output.jsp"));
Can someone point me in the CSSDK to where I could capture the output file?
I found the answer.
First, get or create your CSClient. You can use the examples provided in the cssdk/samples. I tweaked one so that I captured the CSClient in the method getClientForCurrentUser(). Watch out for SOAP vs Java connections. In development, I was using a SOAP connection and for the make_toolkit build, the Java connection was required for our purposes.
Check the following snippet. The request CSClient is captured in the static variable client.
CSSimpleFile sourceFile = (CSSimpleFile)client.getFile(new CSVPath(XSLTExtensionContext.getContext().getOutputDirectory().toString() + "/" + XSLTExtensionContext.getContext().getOutputFileName()));

Categories