从URI或位图获取资源ID [英] Get resource ID from URI or Bitmap

查看:454
本文介绍了从URI或位图获取资源ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正我可以从位图,URI,图像文件路径或文件名查找资源ID?这些图像是由我的应用程序编写。那么,有没有机会,我能找到的资源ID?

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?

基本上,我有存储在我的SD卡(这是由我的应用程序创建)GIF图像。我想这些图像加载到我的浏览器。对于这一点,我使用的 GIF动画视图库。在该文件中, setMovieResource()期待一个int(资源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.

推荐答案

资源ID是自动由Android的资源文件夹内的文件生成( / RES )而已。出该文件夹的,不存在所谓的资源ID这样的东西。

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".

一些Android组件(如查看 S)的的确有的ID,并可以手动设置。然而,手工设置的ID是不是里面的 R.java上市,由Android生成的资源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.

关于库中,还有另一种功能来设置从源图像而不资源ID

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();
}

现在,看着 电影这是包含在Android SDK中类,还有其他功能,取消codeA来源:德codeByteArray的()德codeFILE()德codeByteArray的()接受一个字节数组,而德codeFILE()接受文件路径,可如果你有完整的文件路径中使用。

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.

位图可转换为字节数组,我不知道位图是否支持多层/动画GIF图像的,所以我会避免第一种方法。相反,你可以试试这个:

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

<子>(以上code尚未测试)

这篇关于从URI或位图获取资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆