Skip to content Skip to sidebar Skip to footer

Get Resource Id From Uri Or Bitmap

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?

Solution 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 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:

publicvoidsetMovie(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:

GifMovieViewgifMovieView= ... ; // the reference to the viewStringfilePath= ... ; // the full file path to the GIF imageMoviemyMovie= Movie.decodeFile(pathName);
gifMovieView.setMovie(myMovie);

(The above code is not tested yet)

Post a Comment for "Get Resource Id From Uri Or Bitmap"