How can I add a Bitmap taken from the web (in the app i mean!) to the drawable resources? So i can access the Bitmap using R.drawable.bitmapName...
You can't modify that folder once your app is installed. In that case, you must copy the bitmap to a file and access it from there. Take a look at this other question:
Save bitmap to location
The resources are compiled at the apk building time, so you cannot change it in runtime. If you want to have some identifier you can consider creating a shared preference where you can put in relation the downloaded image file with some id. But it will take some effort from you to implement the functionality.
Related
I have a situation where I need to read an image file (jpg) in a submodule.
The class where I need access to that image is a normal class. So I have two options.
I put that image in the asset folder and try to use assetManager, which doesn't work because I don't have context. My project doesn't have any activity or fragment, because it's a submodule
I put that image in a package. How can I access that image?
The code was written in kotlin.
I will be appreciated any help or suggestions
I want to load Bitmap from Project resources using BitmapFactory.decodeFile(String pathName).But I don't know how pathName should be composed. Here on StackOverflow I found some answers but they are about loading images from SD card. So How can I load Images from Project Resources by their paths(for example from drawable folder). I don't want to use BitmapFactory.decodeResources() because I have a lot of images. And I want to have access to all. With String pathname it will be easier.
But I don't know how pathName should be composed.
There is no pathName, as resources are not files on the filesystem of the device. They are entries in an APK file. You have to use decodeResource().
I don't want to use BitmapFactory.decodeResources() because I have a lot of images.
Having a lot of images does not change matters, nor should it impede your use of decodeResource().
Why don't you use BitmapFactory.decodeResource(R.drawable.test);? It decodes only the resource you want. See here.
I'm currently using shared prefs in android to save some of my settings. This works well, but I came across a little problem with R.
In my shared prefs I'm also saving some image resource IDs so I can acces them later (after the app was destroyed) on a new app start.
Now apparently once I add new resources to my resource tree and build the app again (aka update) the IDs change and I'm accessing a different resource with the ID that I saved before.
Do you have an idea/approach how to save the image resource IDs (or any other identifier) without loosing the link to them?
Thank you very much in advance.
R values will change frequently and are not meant to be stable.
The best solution is for you to be saving something in SharedPreferences that has actual business meaning in your app, then map those values to drawables and other items that you might use for rendering that information. That way, if you decide that you need to refactor and rename those drawables, you don't break your data storage.
If you wish to be lazy or otherwise are willing to stipulate that you can never change the drawable names, you could use something like getResourceEntryName() on a Resources object (you can get one of these via getResources(), called on any Context), to find the name of a resource given its ID, and persist the name. You can then use getIdentifier() on a Resources object to look up the corresponding R.drawable value later on.
You can save your resource name in shared prefs and retrieve the Drawable by :
Drawable drawable = getResources().getDrawable(getResources().getIdentifier(yourDrawableName, "drawable", getPackageName()));
I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).
I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?
new FileOutputStream("/res/drawable/xxx.png");
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream form a file in resources,you should consider creating a raw folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.
I thought that it is impossible to access resources contained in other APK unless content providers are used. However, I stumbled upon an app called Better Keyboard. It uses skins, so I decided to download an example of a skin.
An example skin really surprised me. It contains almost no code, declares only an activity and no content providers. Nontheless it contains resources and they are apparently somehow accessed from Better Keyboard app if the APK with the skin gets installed. So how is it done?
I believe you're looking for
Resources res = mPackageManager.getResourcesForApplication("org.example.foo");
http://developer.android.com/reference/android/content/pm/PackageManager.html#getResourcesForApplication%28java.lang.String%29
Try this
PathClassLoader loader =
new PathClassLoader ("/data/app/com.skin.apk", null,PathClassLoader.getSystemClassLoader());
InputStream f = loader.getResourceAsStream("res/drawable/icon.png");
Bitmap bitmap=BitmapFactory.decodeStream(f);
BitmapDrawable b=new BitmapDrawable(bitmap);
mButton.setBackgroundDrawable(b);
It is possible by using getresoucesforapplication and you need res.getidentifier to retrieve the resource Id. However, It seem not work when retrieving style resource.