Get resource ID from URI or Bitmap

时间秒杀一切 提交于 2019-12-12 02:02:45

问题


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 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)



来源:https://stackoverflow.com/questions/26710240/get-resource-id-from-uri-or-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!