Android - getting the R.raw.value of a file based on filename? - java

I have a file in the res/raw folder called "book1tabs.txt", but in general I will not know what it is called. Then I have to do something like follows:
InputStream in = this.mCtx.getResources().openRawResource(R.raw.book1tabs);
But I want to use a string variable, like
String param = "book1tabs";
And be able to open up that same input stream.
Is there any way to do this?
Thanks

You can do something like this
String param = "book1tabs";
InputStream in = this.mCtx.getResources().openRawResource(mCtx.getResources().getIdentifier(param,"raw", mCtx.getPackageName()));
getIdentifier() will return the id from R.java of particular parameter you passed.
Exactly what it will do is as follow
Maps the Package Name
Navigate to typeDef you provided
Finds the resource name you provided in the typeDef
For more information http://developer.android.com/reference/android/content/res/Resources.html

I find this method to be very useful to pull all sorts of resources by their string names...
#SuppressWarnings("rawtypes")
public static int getResourceId(String name, Class resType){
try {
Class res = null;
if(resType == R.drawable.class)
res = R.drawable.class;
if(resType == R.id.class)
res = R.id.class;
if(resType == R.string.class)
res = R.string.class;
if(resType == R.raw.class)
res = R.raw.class;
Field field = res.getField(name);
int retId = field.getInt(null);
return retId;
}
catch (Exception e) {
// Log.d(TAG, "Failure to get drawable id.", e);
}
return 0;
}
This will return the numeric id (assuming such a resource exists). For the Class pass in R.drawable and for the string whatever it's xml based ID name is.
I always have this method hanging around all my projects for easy access.

Related

Best way to get java.io.File MIME Type in java 8

To get the file mime type i'm trying to use :
String mimeType = new MimetypesFileTypeMap().getContentType(document);
but it always returns application octet stream
When i try to use
String mimeType = Files.probeContentType(document.toPath());
it always returns null
The type of the document that i'm trying to get its mime type, is : java.io.File
Any idea about how can i do ?
I'm already using Spring, is there anything in spring to handle the file mime type ?
Thanks
Finally i used an object "org.apache.cxf.jaxrs.ext.multipart.Attachment" instead of java.io.File
You can easily handle an Attachment
To get the mimeType you can use
public String getMimeType(Attachment document){
String mimeType = document.getDataHandler().getContentType();
return mimeType;
}
To get the document size you can use :
public int getSize(Attachment document){
byte[] documentInByte;
try {
documentInByte =
IOUtils.toByteArray(document.getDataHandler().getInputStream());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return data.length;
}

How to determine the Blob type of all the blobs in a container in Azure?

I know how to list all the blobs in a container but I need to know the type of blob too. Right now I am blindly using the class CloudBlockBlob because of which I am getting an error as (com.microsoft.azure.storage.StorageException: Incorrect Blob type, please use the correct Blob type to access a blob on the server. Expected BLOCK_BLOB, actual PAGE_BLOB.) there's one PageBlob type in the list. Is there a way to determine the type of the Blob?
This is how my code looks like:
public static void getContainerFiles(CloudStorageAccount storageAccount, String containerName) {
String fName = "";
Date lstMdfdDate = null;
try
{
// Define the connection-string with your values
String storageConnectionString = "DefaultEndpointsProtocol=https;" +"AccountName=" + storageName + ";AccountKey="+key;
// Retrieve storage account from connection-string.
storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.getContainerReference(containerName);
StorageCredentials cred = storageAccount.getCredentials();
System.out.println(">>> AccountName: " + cred.getAccountName());
System.out.println("Listing files of \"" + containerName + "\" container");
// Loop over template directory within the container to get the template file names.
CloudBlockBlob blob = null;
for (ListBlobItem blobItem : container.listBlobs()) {
fName = getFileNameFromBlobURI(blobItem.getUri(), containerName);
blob = container.getBlockBlobReference(fName);
blob.downloadAttributes();
lstMdfdDate = blob.getProperties().getLastModified();
}
} catch (Exception e) {
}
}
private static String getFileNameFromBlobURI(URI uri, String containerName)
{
String urlStr = uri.toString();
String keyword = "/"+containerName+"/";
int index = urlStr.indexOf(keyword) + keyword.length();
String filePath = urlStr.substring(index);
return filePath;
}
You can check the type of the blobItem. For example, something like the following:
if (CloudBlockBlob.class == blobItem.getClass()) {
blob = (CloudBlockBlob)blobItem;
}
else if (CloudPageBlob.class == blobItem.getClass()) {
blob = (CloudPageBlob)blobItem;
}
else if (CloudAppendBlob.class == blobItem.getClass()) {
blob = (CloudAppendBlob)blobItem;
}
You can omit the CloudAppendBlob block if you're on older versions of the library, but I'd recommend updating to get the latest bug fixes as well as that feature. Also notice that this removes the need to parse the name out.
Adding just another way to check type #Emily's answer, you can just use "is" operator.
if (blobItem is CloudBlockBlob)
{
...
}
else if (blobItem is CloudPageBlob)
{
...
}
else if (blobItem is CloudAppendBlob)
{
...
}
This looks more readable and faster compared to cast ;)

How to use H2 databases with a custom file extension?

I'm building an application which stores its data in H2 database files, so my save files have the extension .h2.db.
When opening the application, the user has to select which save file should be loaded. To make it easier to recognize these files, is it possible to tell H2 to use custom file extensions?
Looking at the Database URL overview of H2, I can only specify the name of the database. I would prefer a extension like .save over the default .h2.db. Is there a reasonable way to achieve this?
A workaround would be to link the *.save-file to a temporary folder, renaming it to the correct suffix. If this is the only solution, I guess I would go with the default extension.
H2 database supports pluggable file system so with a bit of extra code you can use any extension you want. You just need to create a wrapper, register it and use your own database URL. The wrapper could look like this:
package my.test;
import org.h2.store.fs.FilePath;
import org.h2.store.fs.FilePathWrapper;
public class FilePathTestWrapper extends FilePathWrapper {
private static final String[][] MAPPING = {
{".h2.db", ".save"},
{".lock.db", ".save.lock"}
};
#Override
public String getScheme() {
return "save";
}
#Override
public FilePathWrapper wrap(FilePath base) {
// base.toString() returns base.name
FilePathTestWrapper wrapper = (FilePathTestWrapper) super.wrap(base);
wrapper.name = getPrefix() + wrapExtension(base.toString());
return wrapper;
}
#Override
protected FilePath unwrap(String path) {
String newName = path.substring(getScheme().length() + 1);
newName = unwrapExtension(newName);
return FilePath.get(newName);
}
protected static String wrapExtension(String fileName) {
for (String[] pair : MAPPING) {
if (fileName.endsWith(pair[1])) {
fileName = fileName.substring(0, fileName.length() - pair[1].length()) + pair[0];
break;
}
}
return fileName;
}
protected static String unwrapExtension(String fileName) {
for (String[] pair : MAPPING) {
if (fileName.endsWith(pair[0])) {
fileName = fileName.substring(0, fileName.length() - pair[0].length()) + pair[1];
break;
}
}
return fileName;
}
}
Then you'll need to register it:
FilePathTestWrapper wrapper = new FilePathTestWrapper();
FilePath.register(wrapper);
And use database URL like this:
"jdbc:h2:save:./mydatabase"
Note the "save:" prefix, it should match string returned by getScheme() method. I put a bit more details here: http://shuvikov.net/blog/renaming-h2-database-files
You cannot define a new extension (suffix) for the H2 database name (page file)
In my code i have a similar need (user may select the database) and this is what i do:
File file=new FileImport(shell, "*"+org.h2.engine.Constants.SUFFIX_PAGE_FILE).filePicker();
if (file!=null){
String database=file.getAbsolutePath().
replace(org.h2.engine.Constants.SUFFIX_PAGE_FILE, "");
...
}
NOTE: FileImport is a class writen by me that extends SWT FileDialog: https://code.google.com/p/marcolopes/source/browse/org.dma.eclipse/src/org/dma/eclipse/swt/dialogs/file/FileImport.java
The filename extensions (notice there are many, not just .h2.db) are set in Constants.java:
/**
* The file name suffix of page files.
*/
public static final String SUFFIX_PAGE_FILE = ".h2.db";
And used directly throughout the codebase, e.g. in Database.java:
/**
* Check if a database with the given name exists.
*
* #param name the name of the database (including path)
* #return true if one exists
*/
static boolean exists(String name) {
if (FileUtils.exists(name + Constants.SUFFIX_PAGE_FILE)) {
return true;
}
return FileUtils.exists(name + Constants.SUFFIX_MV_FILE);
}
So no, you cannot specify a custom file extension for the database. You'll be much better off either not worrying about this, or using a FileNameExtensionFilter to limit your user's choices, like JoopEggen suggested.
You could zip up your database files and rename the .zip to some other extension like .save. Then you unzip your .save file in order to get to the .h2.db file. Note this is how Jars work - they're just zip files under the covers.
You might also look at the Game Developer Stack Exchange, and consider posting a question there like "How do I create user-friendly save files" and describe your problem if you really can't expect your users to tolerate .h2.db files.

ResourceTool : to recover a jpg with ResourceNode [velocity]

During my searching, I would like a piece of advice about the following situation :
the guy, on my website, choose a parcel to send, when he validates the choice, some carriers appear as results. Now some carriers have different offers with different logos located in a special directory.
Now the business logic I would like to use is :
If in the directory I find the peculiar logo corresponding to the special offer, I will take the logo to display It in the web page with the special offer.
I choose to do this work with the ResourceTool from Velocity
I have to implement 2 methods getLogo() and getLabel().
The getLogo() will look for the special logo.
I think to use this method to recover the object :
public static ResourceNode getResource(Context context, ResourceType resourceType, String...keys) {
try {
if (null != ResourcesTool.instance) {
ResourceNode resource = ResourcesTool.instance.getResourceSet(context, resourceType);
if (null != resource) {
Deque < String > keyDeque = new ArrayDeque < > ();
for (String key: keys) {
keyDeque.add(key);
}
return (ResourceNode) resource.sub(keyDeque);
}
}
} catch (Exception e) {
BoxtaleLogger.debug("[ResourcesTool.getResource] error: ", e);
}
return null;
}
Now I am searching a example to merely use this method to recover the different .jpg
question 2 : I don't understand what is the meaningful of Context context in this method ?
Then the resourceType is an enum either a String or a picture (the logo in fact)
All right I found It :
public String getLogo(ResourceNode node){
//readable variables
String ope_code = (String)((Instance)get("operateur")).get("ope_code");
String paysDest = ((Instance)db.getEntity("tab_pays").fetch((Integer) get("pz_id"))).get("pz_iso");
String path = node.get(ope_code+"_"+get("srv_code")+"_"+paysDest+".png");
if (path=null){
path = node.get(ope_code+"_"+get("srv_code")+".png");
if (path=null){
path = node.get(ope_code+".png");
}
}
return path ;
}
Now I am testing the method, I will tell you after.

How to avoid many if/else conditions in the method?

I'm in doubts am on right way for one of my methods. This one is a compositor of URI I'm using for my http requests. It just takes data from a separate static class with final Strings, merges them together and includes received from the server token where necessary. It happened all URIs are with token and the only one, authentication, without it. What I've done:
private URI urlComposer(String apiUri, String token) {
URI uri = null;
try {
if(apiUri.equals("POST_AUTH_URL")) {
uri = URIUtils.createURI(null, MyConfig.WEB_SERVER, -1, apiUri, null, null);
return uri;
}
String tmp = apiUri.toString();
String[] array = tmp.split("<token>");
tmp = array[0] + auth.getToken() + array[1];
uri = URIUtils.createURI(null, MyConfig.WEB_SERVER, -1, tmp, null, null);
if (MyConfig.DEBUG) Log.d("Dev", "Constructed url " + uri);
return uri;
} catch (URISyntaxException e) {
if (MyConfig.DEBUG) Log.d("Dev", "urlComposer was unable to construct a URL");
e.printStackTrace();
}
return null;
}
Trying to look in the future I don't like the idea to generate more if/else statements if I would have more special cases like this POST_AUTH_URL. One one hand I want the only one method to be called to construct a URI, on the other I don't want these ifs. What shall I do?
If you always set URIs, only different ones, you can use a map:
uriMap.put("POST_AUTH_URL", URIUtils.createURI(null, MyConfig.WEB_SERVER, -1, apiUri, null, null));
You can access this map later:
uri = uriMap.get(apiUri);
You could theoretically create an enum URI_TYPE with method createURI (it is difficult to derive the actual parameters from the provided code snippet). This way you'd simply invoke this method on a specific enum value, which would have its own specific implementation.
Pls refer this and this for more information. Hope it helps.

Categories

Resources