问题
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.
回答1:
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 View
s) 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)
来源:https://stackoverflow.com/questions/26710240/get-resource-id-from-uri-or-bitmap