Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
252 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...