How to attach an audio file into library? - java

I am developing an application with Android/Java. I need to attach a list of audio files into my java library. So that the developer can use the library and play the audio files. How can I attach the audio files into my Java library and how to call them as file path to play them?

If you are creating an Android Library, you can put your audio file in the raw folder. People using your library can refer to it using com.example.yourlibrary.R.raw.sample_audio_file.
I hope this is what you were looking for. The file will be embedded when the library is created.

If you have specific audioFiles then add them to the special resource folder and refer to them as getResourceAsStream . If you want user to use their own files - provide the variable for the folder path and make a filter to use only specific files, i.e *.mp3.
If they are stored in specific resources folder, why not to create special Accessor class for the developers to use it? Like you will have a *.jar file that will contain files in the separate resource folder with the Accessor to work with them.

Related

How to download file from a URL and save/replace it in a folder using Java 8?

So, I need to download 2 csv files which include pricing details for some AWS services.
https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AWSStorageGateway/current/index.csv
https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonS3/current/index.csv
I need to download the files from the URLs and store it in separate folders to avoid conflicts. Such as, the first index.csv file will be downloaded to a folder AWSStorageGateway and the second one to folder AmazonS3.
Or another approach could be to store those files in 1 folder by changing filenames like AWSStorageGateway.csv and AmazonS3.csv. If the files already exist, it needs to be replaced with the new one.
The project uses Java 8 and Spring MVC. I don't want to use any external libraries. Is NIO package enough to handle this?

Accessing folder in android

I am working on an android app in which I need to detect the language of user's input text.
So using Stackoverflow I found a recommendation of using a java library called langdetect which requires reading languages profiles.
I was able to create a simple plane java project, by adding a directory (folder) inside the java project called "profiles" which contains all the languages profiles.
I couldn't make this work in android since the only 2 ways I know of accessing files in android either by adding the desired files inside "assets" or "rec/raw" but I keep getting error saying file not found.
The method from langdetect jar file that requires reading profiles is the following
String path = "profiles";
DetectorFactory.loadProfile(path);
the above code works in plain java.
Any help guys.
I used the following
Uri.parse("android.resource://com.my.package.my.app/raw");
file:///android_asset
classLoader.getResource("profiles");
and many others in the same style.
The problem is that I don't need to access specific file per say, the only thing I need is a path to the folder that contains the languages profiles, the folder contains 53 files for 53 languages.
path is relative. It does not make sense in Android. You should refere the Internal/External storage with Absolute Path. If you put your data inside the sdcard, for instance, you can retrieve the Absolute Path with Environment.getExternalStorageDirectory(). If you want to embeded your data inside the apk, using the assets or raw folder, you need the AssetsManager in the former case, getResources() in the latter.

A static resource file?

Android uses a static resource file R. This file (at least in eclipse) is automatically updated when ever you add new id's of any sort. How can I create/implement the same feature in a normal java application? Is it as simple as just writing an xml parser and just updating the resource file after the xml is modified?
In a way, yes. You need to create a custom build script/program which runs at the start of each build (before anything else), scans your resource folder files (and if they are XML files it needs to read in the XML files and parse out the string resources or whatever from those), then write it all to a Java file in some manner (e.g. R.string_name = "string value").
Make sure the XML files aren't actually packaged in your .jar, since all that information will be stored inside your Java resources file now.
For things which aren't XML files you could just store the filename as a string in the Java resources file.
You didn't specified the type or the use of the resources. I don't know android, but I'll try to help; If you just need to access some resource in your application you can use properties or resource, there are some differences see this other question Properties vs Resource Bundle

how to get the file and folder information in java?

I'm doing a project that has a module as "File or folder information". It means, I need all open files and open directory details.
E.g.:
If one file "aa.txt" was open by user, and the file is in "bb directory"; the output should be like
aa.txt that file be in open
bb dir be in open
and also, if you change that file, the message appear by that module as aa.txt that file has been modified by this user.
You won't be able to do that in pure java, you'll need to use a custom library (depending on the OS) as file/user/permissions information is hidden form Java to make it portable. You can only check if you can read or write to a file using standard java (you have few more methods as isDirectory, but not much more).
Please have a look at Java's java.io.File API. You will get most of the things there.
For checking if the file has been modified, you can use lastModified() method.
JDK 7 is giving a new The WatchService API and File Change Notification API. You can read about it here.

What is the proper way to use ResourceBundle.getBundle() in an Android application?

I want to know how I can use ResourceBundle.getBundle() in an Android application given that I use it in my Java applications. For example:
I have a properties file, "MyOrg.properties", which I've included in a JAR file named "MyOrg.jar". The path information in the JAR file associated with "MyOrg.jar" is "myorg\" (this is on a Windows system). I build my Java application using "myorg.jar" and note that the following line of code works as expected, which is that it loads the file "MyOrg.properties" from "MyOrg.jar" as a java.util.ResourceBundle:
ResourceBundle resources = ResourceBundle.getBundle( "myorg.MyOrg" );
Next, I place a modified copy of the file "MyOrg.properties" on the file system in the directory "c:\myorg", which is on my Java application's class path. I now rerun my Java application and note that the Resource.getBundle() returns, as expected, a bundle for the modified copy of "MyOrg.properties" (the one that is on the file system).
The behavior I've just described is what I would like to be able to accomplish with an Android application. However, ResourceBundle.getBundle() throws a MissingResourceException; it fails to find "MyOrg.properties" in either the JAR file or as a stand-alone file.
So I have two questions:
1) - Is it possible for an Android application to retrieve a resource from a JAR file using ResourceBundle.getBundle() (or any other way for that matter)?
2) - Is it possible for an Android application to retrieve a resource from a file using ResourceBundle.getBundle()?
Regarding 2): I'm using the nbandroid plugin with NetBeans 6.7.1 and I've placed copies of "MyOrg.properties" on the file system as follows prior to building my Android application:
MyProject
-- build
-- classes
myorg (directory contains "MyOrg.properties")
...
src
myorg (directory contains "MyOrg.properties")
you need to make sure the properties file makes it to the .apk file. your best bet is probably in res/raw/ or assets/. see also PropertyResourceBundle's constructors (since it's easy to get hold of an InputStream).

Categories