I have an image that has to be shown by imageview what should be the perfect directory for loading it as bitmap from string .I have tried putting it resource folder but failed.It would be great if you provide folder hierarchy.
Resource images should be stored in a res/drawable variant according to these guidelines.
By variant I mean any density dependant (-hdpi, -xhdpi...), orientation dependant (-land, -port), or any other qualifier or combination defined in the Table 2 of the linked document (mind the order of the qualifiers, it does matter as explained in the document).
It is a best practice to consider /res/drawable as a fallback should no better qualified directory apply.
can you post your code? Typically I put my images in drawable folder. By displaying it in imageView you should get the id of the image you wanted to place.
Related
I am having a rather annoying scenario where I work with a lot of drawable shapes, many of which are slightly changed variants of others. The annoying thing is, when I paste the drawable, it automatically defaults to a random resource folder. See bellow photo. I want it to go directly to the standard drawable folder, not some other density drawable folders without having to manually change the folder every single time.
Is this even possible?
You can change the View from Android to Project if you are copying & pasting multiple images in a row. You can simply paste it to the drawable folder.
in Delphi, I add image resources to the project (via project > resources and Images), however i need for one java function to give a resource ID in the application's package of the drawable to use. Is it possible to retrieve a resource ID (integer) from my delphi resource file or name ?
If not, how in delphi we can add a custom resource image and retrieve it's resource ID ?
Getting a Resource ID
To retrieve the resource ID of a resource:
id := TAndroidHelper.GetResourceID('my_image', 'drawable');
If you are using an older version of Delphi you may need to use the alternate, older approach:
id := TAndroidHelper.Context.getResources.getIdentifier(StringToJString('my_image'), StringToJString('drawable'), TAndroidHelper.Context.getPackageName);
The shorter, more recent (and convenient) GetResourceID function is merely a wrapper around the earlier longer winded version.
In either case, my_image is the filename (without extension) of the image resource whose ID you need.
The drawable parameter tells the helper that it is a drawable resource ID that you are asking for (as opposed to a string or layout, for example).
Adding Image Resources
To add a custom image resource to your project use the Deployment option of the Project menu in the IDE.
Add your file and set the remote path to a suitable drawable resource folder, e.g.:
res\drawable
res\drawable-xxhdpi
etc
You could put your own scaled versions of the image in each drawable folder for different resolutions. The remote_name must be the same in each case. The specific drawable folder determines the applicable device resolution.
Alternatively you could simply provide one suitably high resolution file. You can place this in either an appropriate high-resolution folder (e.g. drawable-xxxhdpi) or simply in drawable.
Either way, Android will auto-scale the images at runtime for other device resolutions as necessary.
There are lots of Android references on the subject of drawable scaling, alternative versions for different display types etc, including the Android documentation itself of course.
Referencing your comment, there are however no XML files required for adding drawables that are straightforward images.
Additional XML may be necessary for other types of drawable resources, but that will very much depend on the nature of your specific needs.
Bringing It All Together
In the screenshot below the highlighted entry is one that I have added for a PNG image resource. The file is in my project folder so there is no local path (the first column).
The filename is appstore.png and I have configured the deployment to place one copy of that file in the res\drawable' folder of the application when it is deployed (theremote` folder).
This file will rely on auto-scaling for display on different resolutions devices.
To get the resource ID of that resource I then simply write:
id := TAndroidHelper.GetResourceID('appstore', 'drawable');
I've had an app developed and have the responsibility of maintaining it, which means learning the Eclipse ADT environment. Nearly 20 years in web dev gives me some comfort, but this is certainly a new experience.
In one of the screens shown in the Graphical Layout window, a graphic source is indicated in the Properties panel as:
Src #drawable/ordo_search
ordo_search, obviously being the name of the PNG graphic, drawable appearing to be the folder.
But there are 4 folders holding graphics for this app, all beginning with the word drawable. They are:
drawable
drawable-hdpi
drawable-large-mdpi
drawable-sw600dp-hdpi
By altering this particular image and seeing the change come up in the Graphical Layout, I've determined that this graphic resides in the one called drawable-sw600dp-hdpi. In other areas of the app, I've determined in the same way that graphics are being pulled from any of the 4 folders, but in all cases the properties source paths all read the same: #drawable
Somewhere that #drawable attribute is being told an absolute path to where that graphic is, and that's what I need to find: where would I find and edit the path to that, or any, graphic?
Obviously I'm just getting to know the environment, so bear with me if you would.
It's not possible to get the path
This path will differ from device to device due to different dpi's of devices, it can point to any of the 4 folders you defined. If you want the drawable image you can get it via code by using getResources().getDrawable(R.drawable.yourdrawablename);
This will return your drawable and you can use it to display in a ImageView or where ever you want.
I've read the docs about Android Resources and I think I've understood the best-matching logic used by android to identify in which directory a particular resource should be searched first.. Supposing that drawable-hdpi, drawable-en-port and drawable-en* match the current device configuration, drawable-en-port is the best matching directory.. My question is, if a drawable is not found in drawable-en-port, does the system look directly in drawable or does it look in the second-best-match drawable-en and then in the third best match drawable-hdpi and so on until it reaches drawable? I suppose it works this way, but I did not find it explicitly said in the docs (unless I've read them too quickly and you will surely kill me :) ).
Yes it looks first in the drawable-en, then drawable-hdpi because language qualifier has higher precedence. If value was still not found drawable directory is searched. It is in accordance with:
How Android Finds the Best-matching Resource
If drawable-en contains matching resource then drawable-hdpi and drawable would be eliminated based on step 4 of the algorithm.
I read the intro portion
assume the following drawable directories each contain different versions of the same images
In your example Android would ignore the drawable-en-port folder because it does not have that drawable name in it. It would only look in the folders that have that drawable, therefore selecting the best matching folder. It would only select 'drawable' if there were no folders with a matching configuration and image.
On my application I have many pictures which I have grouped in folder under res/drawable. But Android dosen't let me access them trough "R". Is there a way to access those folders.
This is the Code im using for.
ImageView iv = new ImageView(conext);
iv.setImageResource(R.drawable.**smiley.666**);
I marked the part which I can't access.
Thx in Advance
safari
No, Android does not allow subfolders under /res/drawable: Can the Android drawable directory contain subdirectories?
You can however, add all image files under /drawable and then access them programmatically via:
int drawableID = context.getResources().getIdentifier("drawableName", "drawable", getPackageName());
iv.setImageResource(drawableID);
Where drawableName is a name of the drawable file, i.e. myimage if image file is myimage.jpg.
The code above will get image resource with id R.drawable.drawableName.
R.drawable.xxx is just a reference that the Android SDK generates in your R.java file. You are trying to make a sub-folder in your res-folder. The SDK can't generate a reference to any of your sub-folders, you have to put it in the predefined folders drawable-hdpi, -ldpi and -mdpi.
If you dont know how this works. I'll sum up. Android runs on a lot of different devices with a lot of different screen resolutions. With these three folders, you can have the same picture in three resolutions and depending on the device you are running your app on, one of these three folders will be used. You can read more here:
http://developer.android.com/guide/practices/screens_support.html
You can't create folders in res/drawable the system doesn't recognize those.
you need to save your image first and then make a xml for them so you can access it through R.your_pics
context.getResources().getDrawable(R.drawable.progressbar1);
Android - Open resource from #drawable String