I am using HorizontalScrollView in order to create some Gallery-like option and right now I am getting the images from an folder in the external drive. But I figured that I don't need, also shouldn't do that since the images I wanted to show is predetermined images. So I will simply change the directory to the drawable folder but I am not sure which path to go. So right now I am getting the directory with this:
String path1= Environment.getExternalStorageDirectory().getAbsolutePath();
I found two more methods for directory retrieval but I am not sure which one to use.
String path2 = Environment.getDataDirectory().getAbsolutePath();
String path3= Environment.getRootDirectory().getAbsolutePath();
First one returns "/mnt/sdcard", second returns "/data", third one "/system".
Which one should I use in order to reach /drawable folder inside this way?
Which one should I use in order to reach /drawable folder inside this way?
None of them. Those are all for files on the filesystem. Drawable resources are not files on the filesystem.
Use getResources().getDrawable() to retrieve a Drawable given its ID (e.g., R.drawable.foo).
Related
Is it possible to get the path of an image in string form in android like
res/drawable/image.png
Or is their any other way for getting string path of an image that is placed in drawable.
No, it is not.
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
e.g. will give you the default launcher icon. It is not possible to get the exact path for an image , that is stored in drawable.
Why not? When you compile your app to an *.apk file, all resources (ok, except from them in /raw) are compiled as well. You can only acces them, using their R. id.
Solution? Not really, you could copy them to a location on the sd card for example. Now you know the location :)
its not possible to get path of drawable image directly , but you can get this by storing it in internal memory or sdcard of device.For this i have written blog.. check out the link
here
i am sure it will help you.
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
Is it possible to create our own folder in drawable and put all my images in that folder as:
"drawable\Myimages\p.png.."
If it possible than how can i use this images in my activity..gnerally we use "R.drawablw.p" but now how that can be written because now my images is in drawable\Myimages directory
please suggest me..
with regards
Anshuman
No, you can't create your own file hierarchy in drawable directory.
But you can see in http://developer.android.com/guide/topics/resources/providing-resources.html:
However, if you need access to original file names and file hierarchy,
you might consider saving some resources in the assets/ directory
(instead of res/raw/). Files in assets/ are not given a resource ID,
so you can read them only using AssetManager.
If you have very many resources, it can be useful for you.
No, the resources mechanism doesn't support subfolders in the drawable directory.You can't used.
I am working on an ANDROID project where I have a lot of images and I want to organize it.
So I need to create folder inside the drawable folder.
But I don't know how to reference it in my program.
For eg: I have an image named "image.png" and I want to place it in drawable/myicons/image.png
or anywhere inside the res folder.
This is duplicate question
You can not create folder inside drawable
the resources mechanism doesn't support subfolders in the drawable directory
However you can use the assets folder and have sub directories in there and load images that way.
I am new to Android development. To implement a photo gallery app, tutorial said to add images to res/drawable and access them via R.drawable.image1 .. etc. But there is no drawable folder default in eclipse, then I created a one and add images. But cannot access images, there is no option in R.drawable. as "image1"
Any idea ?
you should create "drawable" folder under "res", and you'd best place png image files in that folder. When accessing these image files programmingly.set an image for an ImageView for example:
Drawable imgDrawable=getResources().getDrawable(R.drawable.yourImageFileName));
imgView.setImageDrawable(imgDrawable);
Sometimes the drawable folder is not created by default Instead it will have "drawable-hdpi","drawable-mdpi" and "drawable-ldpi".These folders are for placing images for various screen densities to support different resolutions.Your "drawable folder" is for baseline medium density.
In your case create a folder named "drawable" under "res" and place your image inside that folder and you will be able to access it using R.id.imagename .Try refreshing the project and check whether you have created the drawable folder under the res folder.Also check the case of the folder name that you have created.
I faced same problem , but when i added those images again in jpg format it worked.
Might work for you too.
My System was based on windows 10 & was using android studio 4