How to convert Int to constant for res/raw? - java

InputStream inputStream = getResources().openRawResource(R.raw.ac);
here, ac is constant file name from res/raw.
int books = cursor.getColumnIndexOrThrow(DictionaryDatabase.BOOK_DETAILS)
is an int contains name: ac also.
Is it possible to use
InputStream inputStream = getResources().openRawResource(Int books);
???
If yes how??

If I understand correctly, you want to get the value of the R.raw.xxxx field having the name xxxx in a variable. This can be achieved with a code like this:
String resName = "mybook";
int resID = context.getResources().getIdentifier(resName, "raw", context.getPackageName());
InputStream inputStream = getResources().openRawResource(resId);
Here, context is an instance of Context in your app, in an activity, this would be this.

Related

How to split the string a android?

I am working on android application. I am getting the image from gallery. Also I am getting the image path from gallery. Now my requirement is I want to get only the image name with the extension . How can I do that? Please help me.
String imgpath = "/mnt/sdcard/joke.png";
The image extension can be anything joke.png or joke.jpeg. I need to get the image name with extension finally.
i.e I want to split the above string and get only joke.png.
How can I achieve that? Please help me in this regard.
String imgpath = "/mnt/sdcard/joke.png";
String result = imgpath.substring(imgpath.lastIndexOf("/") + 1);
System.out.println("Image name " + result);
Output :-
Image name joke.png
You should read How do I get the file name from a String containing the Absolute file path?
You can do that in Android like in any Java program:
String[] parts = imagepath.split("/");
String result = parts[parts.length-1];
String s[] = imgpath.split("/");
String result = s[s.length-1];
String imgName = imgpath.substring((imgpath.lastIndexOf("/") + 1), imgpath.length());
You can get this with Regex too, if that is the hammer you have in your hands:
String fileName = null;
Pattern pattern = Pattern.compile("(^|.*/)([^/]*)$");
Matcher m = pattern.getMatcher(filenameWithPath);
if(matcher.matches()) {
fileName = matcher.group(2);
}
But don't be tempted to do this. This is less readable, and probably even slower than the other methods.

read tags from mp3 file

I have a code that reads the mp3 tag, it's the last 128 bytes of the file
try{
final int TAG_LONG = 128;
fileInputStream = new FileInputStream(file.getAbsolutePath());
byte[] bytesSong = new byte[128];
byte[] byteTitleArry = new byte[length];
for (int i = 0; i < 30; i++) {
byteTitleArry[i] = arrayTag[3+ i];
}
String title = new String(byteTitleArry);
}
this code works just fine when the title tag (or enything else) is written english chars, but when the tag is a value in non-english chars i get the wrong value, so i used:
String title = new String(byteTitleArry, "ISO-8859-8");
but this is not generic enough, is there a way to find out what is the tag unicode?
Try using an ID3 library instead, e.g. JAudioTagger.
It seems that the ID3v1 you are trying to use does not hold any i18n information. ID3v2 however does. Source: http://en.wikipedia.org/wiki/ID3

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

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.

Android: How to retrieve file name and extension of a resource by resource ID

I have the following:
getResources().getResourceEntryName(resourceId);
The problem is, that it retrieves only the file name without the extension.
For example, if I have the following res/drawable/pic.jpg, the
getResources().getResourceEntryName(resourceId);
is returning the value "pic". The extension .jpg is missing.
To get "res/drawable/pic.jpg" you could use this:
TypedValue value = new TypedValue();
getResources().getValue(resourceId, value, true);
// check value.string if not null - it is not null for drawables...
Log.d(TAG, "Resource filename:" + value.string.toString());
// ^^ This would print res/drawable/pic.jpg
Source: android/frameworks/base/core/java/android/content/res/Resources.java
You can do so
Field[] fields = R.raw.class.getFields();
for (int count = 0; count < fields.length; count++) {
// Use that if you just need the file name
String filename = fields[count].getName();
Log.i("filename", filename);
int rawId = getResources().getIdentifier(filename, "raw", getPackageName());
TypedValue value = new TypedValue();
getResources().getValue(rawId, value, true);
String[] s = value.string.toString().split("/");
Log.i("filename", s[s.length - 1]);
}
This should be the best solution:
TypedValue value = new TypedValue();
getResources().getValue(resourceId, value, true);
String resname = value.string.toString().substring(13, value.string.toString().length());
resname = "pic.jpg"
Short answer: You can't.
Another way to do this, would be to put your graphics inside the assets folder.
Then you can access the Files directly, without your App needing any permission.
You can, for example, do so in your Activity:
AssetManager am = this.getApplicationContext().getAssets()
InputStream is = am.open(foldername+"/"+filename)
Bitmap myNewImage = BitmapFactory.decodeStream(is);
I hope that this will accomplish what you had in mind.
UPDATE: it seems it is indeed possible, see Aleksandar Stojiljkovic's answer instead.

Using a String to change a Bitmap

I have the following problem:
String explosion_type = this.prefs.getString("explosion_type", "ring_explosion");
BitmapFactory.Options optionsExplosion = new BitmapFactory.Options();
optionsExplosion.inPurgeable = true;
this._explosionSprite = BitmapFactory.decodeResource(_context.getResources(),
com.forwardapps.liveitems.R.drawable.explosion, optionsExplosion);
I'm trying to use a String in place of the resource name, that way I can hotswap resources in the preference menu. This method causes the app to crash.
What is the proper way of implementing a situation like this? (Without making it too complicated or using if statements)
Use method getIdentifier on Resources object to fetch id of a drawable:
String explosion_type = this.prefs.getString("explosion_type", "ring_explosion");
Log.i("imagename", explosion_type);
BitmapFactory.Options optionsExplosion = new BitmapFactory.Options();
optionsExplosion.inPurgeable = true;
int imageResource = getResources().getIdentifier(explosion_type, "drawable", getPackageName());
this._explosionSprite = BitmapFactory.decodeResource(_context.getResources(), imageResource, optionsExplosion);

Categories

Resources