I'm trying to click through several full screen images and depending on what button I click, come up with a file name to display next.
For example A_UP0_LEFT2_B
would go to B_UP0_LEFT0_?
the syntax of my file names are currentscreen_xcoordinate_ycoordinate_destinationscreen
The question mark will be another letter and I'm wondering if there is a way for me to look through my drawables directory programatically if I have the string "B_UP0_LEFT0_" and find an image to display next, let's say it's "B_UP0_LEFT0_C.jpg"
short version: if I have a string "B_UP0_LEFT0_" and there is only one file with that string but it also has a "C" at the very end of it, is there any way for me in android to use that string to find the full file from the drawable folder? ie. "R.drawable.B_UP0_LEFT0_C"
You can get all the drawable names via this snippet:
Field[] drawableNames = R.drawable.class.getFields();
for (Field f : drawableNames) {
try {
System.out.println(f.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
from there I guess a Regex match or simple startsWith() or contains() will do the trick for you.
Also to find a drawable by its name using something like this:
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(drawableName, "drawable", getPackageName()));
Related
I am using a third party library for splash screen. I got it from https://github.com/ViksaaSkool/AwesomeSplash. I have followed the whole tutorial but i am getting error while setting string path. It has a line
"configSplash.setPathSplash(SyncStateContract.Constants.DROID_LOGO); //set path String". Android studio is unable to resolve DROID_LOGO and i am getting error. I replaced DROID_LOGO with DATA and got this error.
As suggested on the post i have written this code.
public class splashex extends AwesomeSplash {
//DO NOT OVERRIDE onCreate()!
//if you need to start some services do it in initSplash()!
#Override
public void initSplash(ConfigSplash configSplash) {
/* you don't have to override every property */
//Customize Circular Reveal
configSplash.setBackgroundColor(R.color.colorPrimary); //any color you want form colors.xml
configSplash.setAnimCircularRevealDuration(2000); //int ms
configSplash.setRevealFlagX(Flags.REVEAL_RIGHT); //or Flags.REVEAL_LEFT
configSplash.setRevealFlagY(Flags.REVEAL_BOTTOM); //or Flags.REVEAL_TOP
//Choose LOGO OR PATH; if you don't provide String value for path it's logo by default
//Customize Logo
configSplash.setLogoSplash(R.mipmap.ic_launcher); //or any other drawable
configSplash.setAnimLogoSplashDuration(2000); //int ms
configSplash.setAnimLogoSplashTechnique(Techniques.Bounce); //choose one form Techniques (ref: https://github.com/daimajia/AndroidViewAnimations)
//Customize Path
configSplash.setPathSplash(SyncStateContract.Constants.DROID_LOGO); //set path String(**I am getting error here**)
// configSplash.setOriginalHeight(400); //in relation to your svg (path) resource
configSplash.setOriginalWidth(400); //in relation to your svg (path) resource
configSplash.setAnimPathStrokeDrawingDuration(3000);
configSplash.setPathSplashStrokeSize(3); //I advise value be <5
configSplash.setPathSplashStrokeColor(R.color.colorPrimaryDark); //any color you want form colors.xml
configSplash.setAnimPathFillingDuration(3000);
configSplash.setPathSplashFillColor(R.color.colorPrimaryDark); //path object filling color
//Customize Title
configSplash.setTitleSplash("My Awesome App");
configSplash.setTitleTextColor(R.color.colorAccent);
configSplash.setTitleTextSize(30f); //float value
configSplash.setAnimTitleDuration(3000);
configSplash.setAnimTitleTechnique(Techniques.FlipInX);
configSplash.setTitleFont("fonts/myfont.ttf"); //provide string to your font located in assets/fonts/
}
#Override
public void animationsFinished() {
//transit to another activity here
//or do whatever you want
}
}
The error i am getting is
java.lang.RuntimeException: Font asset not found fonts/myfont.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
I searched various links and all suggested to add assets folder but i dont know where to make assets folder and what to store in that.
please help me with all the details about assets and fonts.
Still i am getting this.
You have to create folder named fonts according to the following path:
your_android_project_folder > app > src > main > assets > fonts
Now within your fonts folder,you need to store your myfont.ttf file.
I had the same problem and here is how I solved it :
For the missing DROID_LOGO, you can rewrite the line from this:
configSplash.setPathSplash(SyncStateContract.Constants.DROID_LOGO);
to this :
configSplash.setPathSplash(SyncStateContract.Constants.DROID_LOGO);
Here, make sure you add to your project the Constants.java class which can be found here
And for the missing font, go to the line
configSplash.setTitleFont("fonts/myfont.ttf"); //provide string to your font located in assets/fonts/
and replace myfont.ttf with another font present in your assets/font folder for example fonts/diti_sweet.ttf(which can be found on the AwesomeSplash repository) and averything might work fine
Comment out this line:
configSplash.setPathSplash(SyncStateContract.Constants.DROID_LOGO);
and rename myfont.ttf to your font in folder assets.
I am trying to read images from Assets.In Assets i have been using a-z folders and these folders contains images with the starting name of folder like if there is folder named YImages
then images in this folder image's names are also starting with Y word.
I am using this function to get images from Assets:
try {
pics = Drawable
.createFromStream(
getAssets().open(
imageFolder + wordString.get(index)
+ ".jpg"), null);
picture.setBackgroundDrawable(pics);
picture.refreshDrawableState();
} catch (IOException e) {
e.printStackTrace();
}
Theres a space at your image path. java.io.FileNotFoundException: yImages/ Year.jpg
Try trimming the File name.
Just change all the image file name with the small letters do not use the capital letter in image file name . i.e badger_thumbnail.jpg,badge_thumbnail.jpg,year.jpg like this. Also keep in mind that there is no space in your any image name.
I have images placed in following path
res>drawable>Images>Currencies>[All images]
I have made a custom spinner Adapter.
viewHolder.flag = (ImageView) view.findViewById(R.id.UICurrencyCurrencyFlag);
viewHolder.flag.setImageDrawable(drawable);
How can i set these images in my ImageView. Images format is png
Best Regards
You have 2 choices:
Put all images in drawable folders without subfolders.
Put all images in asset with your folder structures.
The differences are:
If you go for approach 1, you can retrieve the resource by id. (R.drawable.*)
If you go for approach 2, you get to maintain your folders, but you need to read the resource as raw.
To open file in asset folder, you can refer to a previous post:
Opening a File from assets folder in android
Suggestion:
Rename your file as images_currency_image1.jpg to avoid subfolders but keep unique-ness.
I am not sure if this help in your situation.
You don't really need to make subfolders inside drawable folder. And even if you do, you can't access the contents inside it. And it doesn't even make sense. Why you need subfolders when android is taking care of giving you the drawables by their id. You place your images directly into res/drawable folder like res/drawable/[All images]. And then try to access by R.drawable.UICurrencyCurrencyFlag and not R.id.UICurrencyCurrencyFlag. All the best.
if u place in assets folder u can access in following manner
InputStream bitmap;
try {
bitmap = getAssets().open("icon.png");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
ImageView img = (ImageView) findViewById(R.id.myId);
img.setImageBitmap(bit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am newbie to android don't know much about it. I have imported a image to my project and when i tried it check it using File that the file exists or not.
This is the code i have used..
existingFileName="res/drawable-ldpi/login.png";
File f= new File(existingFileName);
// Log.d("Image path",);
if(f.exists())
{
Log.d("EXISTS", "====File Exists===");
}
Still it is showing me no image exist.
thanks for help in advance,
aby
If you are putting your images in drawable folder, I don't think you can access them like this. Try this
Drawable image= getResources().getDrawable(R.drawable.<id>);
id will be your file name.
If you store the image in the "res/drawable" folder you can do:
Drawable drawable = getResources().getDrawable(R.drawable.name)
See http://developer.android.com/reference/android/content/res/Resources.html#getDrawable
For the case you store your image in the assets folder, you need to use getAssets() ( http://developer.android.com/reference/android/content/Context.html#getAssets() )
You can do something like this:
InputStream is = getAssets().open(imageName);
BufferedInputStream buf = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(buf);
Remember that for both case you need to have a Context (ie an Activity)
There is no need to check the existence of an asset file before you read it. AssetManager takes care of it. If file is missing, it throws an exception. Your coding practice should be to always enclose that part of code in try catch and split the logic. Extra code is not needed. If you try an open a file that doesn't exist you will get an exception.
Though, if you still want to do that manual job, try below code.
AssetManager mg = getResources().getAssets();
try {
//do all considering file exists
mg.open(pathInAssets);
} catch (IOException ex) {
//do all in case file is missing
}
If your file is located in assets/folder/file.ext, then pathInAssets would be "folder/file.ext"
Check for file existence in androids assets folder?
How to check Android Asset resource?
Usually if you use res/drawable, your ressource identifier is compiled into the R.java file. So if you use an identifier from there, you can be sure the file IS there, otherwise you cannot compile your project.
If however you like to exchange R.java later and really need to list the ressources in res/drawable, you can do it with java Reflection, examining the R class like this:
Class<?> c=R.drawable.class;
Field[] fs=c.getFields();
for(Field f: fs)
Log.v("test", f.getName());
You will then get a list of the ressources, without the file extension. The Ressource Identifier (an integer) for every ressource is then read with int id=f.getInt(null);.
Use the DDMS perspective in eclipse to browse for the image on an emulator running your app.
If you get this error.. "Cannot make a static reference to the non-static method getResources() from the type ContextWrapper"
Try Drawable image=Classname.this.getResources().getDrawable(R.drawable.<id>);
here Classname.this means the ClassContext/Application Context.
i have a subfolder in the Assets folder called images where i store my images(of course) :) The things is that i want to get the name for the images which i'm getting but the problem is that i'm also getting other unknown names like: "android-logo-mask.png" which i guess are android's default images. Is there a way i can skip this "android default images" to get only the names of my images? My plan is to save this names in a database to use it as reference for showing the images later on an ImageView. Is it a good idea to use the image name to show the images? Here is some code if it needs:
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();
ImageHelper i = new ImageHelper();
i.readImages(context);
}
public class ImageHelper {
public ImageHelper(){}
public void readImages(Context context){
AssetManager am = context.getAssets();
try {
String[] getImages = am.list("images");
for(String imgName : getImages){
Log.e("IMAGE NAME----->", imgName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks in advance.
Use some other name for images folder like "MyImages". It should work fine then.
"images" should be a path like "/path/assets/images/image.png"
It is very interesting. Even if you don't have an assets/images directory, when you use:
myImageList = Arrays.asList(getResources().getAssets().list("images"));
you'll see android-logo-mask.png and android-logo-shine.png listed in the results.
That led me to wondering where those actual image files live in the Android code. I don't know the code base at all, but:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/assets/images/
lists those two in an assets directory under res. (Different from the assets directory where I store my project assets, that's not under res).
Being required to provide a path to an individual file for the AssetManagers list method (which returns an array) just doesn't make any sense. And the method documentation says:
Return a String array of all the assets at the given path.
It makes me wonder if there's an error in the list method, or if there was some other reason it was designed to return particular system assets when an asset list is required.
In a similar fashion, if I use:
myAssetList = Arrays.asList(getResources().getAssets().list(""));
I see "sounds" and "webkit" included, which don't correspond to any existing subdirectory of my assets.
If it occurs that the system adds files in this "images" folder, why not create a new folder for your images that you are sure to contain only your files ?