Get resource ID from URI or Bitmap - android

Is there anyway I can find resource ID from Bitmap, URI, file path or file name of an image? These images are written by my app. So is there any chance I can find the resource ID?
Basically, I have GIF images stored in my SD Card (which are created by my app). I want to load those images to my viewer. For that, I am using GIF Movie View library. In that file, setMovieResource() expecting an int (resource ID). But I have all other info like bitmap, absolute path.

Resource IDs are automatically generated by Android for files inside the resource folder (/res) only. Out of that folder, there is no such thing called "resource ID".
Some Android components (such as Views) do have ID and can be set manually. However, manually-set ID is not listed inside R.java, the collection of resource IDs generated by Android.
But in your case, generated images don't have ID, so it's impossible to get something that doesn't exist beforehand.
Regarding the library, there is another function to set the image from source without resource ID:
public void setMovie(Movie movie) {
this.mMovie = movie;
requestLayout();
}
Now, looking at the Movie class which is included in Android SDK, there are other functions to decode a source: decodeByteArray() and decodeFile(). decodeByteArray() accepts a byte array, and decodeFile() accepts a file path, which can be used if you have the full path to the file.
While Bitmap can be converted to byte array, I'm not sure whether Bitmap supports multiple layers/animation of a GIF image, so I'll avoid the first approach. Instead, you can try this:
GifMovieView gifMovieView = ... ; // the reference to the view
String filePath = ... ; // the full file path to the GIF image
Movie myMovie = Movie.decodeFile(pathName);
gifMovieView.setMovie(myMovie);
(The above code is not tested yet)

Related

How to properly use BitmapFactory.decodeFile(String pathName)

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.

Image comparator returns false

So, I'm using a file sharing app on Android. It creates a duplicate copy which is uploaded to it's server.
PROBLEM
The following code works for a duplicate copy I manually create. That is, I long press and copy the file into the same directory with a File Manager. Then my function returns true. When it compares the duplicate image due to the app and the original image, I get false.
MD5-checksums are different so that is out of the options.
CODE
public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight()
* bitmap1.getRowBytes());
bitmap1.copyPixelsToBuffer(buffer1);
ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight()
* bitmap2.getRowBytes());
bitmap2.copyPixelsToBuffer(buffer2);
return Arrays.equals(buffer1.array(), buffer2.array());
}
Here are the images :
Original image -
Duplicate image created by the app -
My code currently returns false while comparing these two images.
How do I get the code to return true?
Your problem is due to artefacts created by JPEG compression, if you can always keep the images in PNG then your problem is most likely solved. If you can't do that, then you need a better algorithm to compare the images. This is exactly the same problem discussed at Comparing image in url to image in filesystem in python
For instance, running the algorithms mentioned in the earlier discussion, we get a similarity of more than 99%. With that similarity value, you can say the images are the same.

Access to resource folder route

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.

Android: Loading multiple images

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?

Adding a bitmap to the drawable resources

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.

Categories

Resources