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.
Related
I'm trying to set up ARCore scene with Sceneform. There are this piece of code:
(supportFragmentManager.findFragmentById(R.id.single_player_ar_fragment) as ArFragment)
.setOnTapPlaneGlbModel(link)
It loads 3d-model with .glb extension by link, which is String. I can't understand where to put .glb file and how get a path to it. I tried to put it in assets or res folders, but in 2 hours i couldn't find how to get String path to it.
I know that these folders have open() methods, which returns InputStream, but setOnTapPlaneGlbModel() needs String with a path as an argument.
I had assumed that the files are in the same location as the sample:
https://github.com/SceneView/sceneform-android/tree/master/samples/3d-model-viewer/src/main/assets/models
Based on comment thread:
setOnTapPlaneGlbModel(Uri.parse(link).toString()) seems to resolve to the correct asset location and fix the issue.
References:
https://github.com/SceneView/sceneform-android/blob/master/samples/3d-model-viewer/src/main/java/com/google/ar/sceneform/samples/sceneviewbackground/MainActivity.java
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 am developing an application which creates menu with each menu item having its own icon. I have too many PNG files for each menu item. In future i will be updating menu items from WebService which will add to these png files.
What is the most efficient way to store this data(png files). I don't want to keep it in drawable folder as I can't update contents of drawable folder after i have shipped my app. If i am storing it on External Memory, How do i achieve that? Where exactly should i copy these png files and use them as resources in my code.
Thanks
Store them as BLOBs in sqLite.
You can write to private space assigned to your app using this http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int) method and read back with it's sibling.
InputStream is = your input stream
OutputStream os = mContext.openFileOutput("image.png",Context.MODE_PRIVATE);
Utils.copyStream(is,os); //Your own steam copy here
is.close;
os.close;
I have recently started developing a game in android, never used it before and was wondering if there is a simple way of getting a set of images loaded into the application. As my current implementation involves basically
Creating an int[] array,
Storing the values of each drawable into this array, (now this has to be hand coded, so if I add any more images it has to be added programmitically)
Then itterating through each item in the array and calling BitmapFactory to get the resource.
(Unfortunately I don't have the code with me as it is at home and I am at work, but that is the jist)
So 2 questions, is there a way of getting the drawables without having to put in each item manually to the int[] - ie looking for perhaps a file name prefix and then only loading the resource with the prefix?
Which leads me to my second question because I more than just these images in my drawable resource directory, is there a way to add extra organisation (folders) to manage the files better. As currently I have loads of images within the drawable file and how would I reference these sub folders/images.
You cannot have sub folders within the resources structure. Android depends on the folder layout to determine which resource to use in what condition (localization, different screen resolutions, etc).
I'm not sure why exactly you are trying to load up a whole bunch of images, but there are a couple of (slower) methods that allow you to look up a resource by string name. If you used a naming convention for your images you could look them up that way via [Resources.getIdentifier()][1]. However, in a game performance likely matters, so you are probably better off with a more manual approach using the int IDs directly since it is much more efficient.
[1]: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
I am uploading a load of images as they will be shown to user for different items. Its not a system where responsiveness is critical so its okay in terms of what I want. Though...
public int getIdentifier (String name, String defType, String defPackage)
Since: API Level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
They suggest using the resource id but if I want to add a file later on then I have to re-compile the app to include the extra file, this is where it bugs me, as the pic gets associated to an item that I have in a string array. So I can add items to the array but not the images without a change of the code.
Surely there is a function to fix this?
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.