在 android 中,我试图从我下载并复制到 res 下的可绘制文件夹的 jpeg 创建位图。
附上我的代码:
public void draw(Canvas g, Resources res, int x, int y, int w, int h) {
Bitmap im = BitmapFactory.decodeResource(res, R.drawable.green_dragon);
Bitmap im = BitmapFactory.decodeFile(R.drawable.green_dragon);
g.drawBitmap(im, null, new Rect(x*w, y*h, (x*w)+w, (y*h)+h), new Paint());
}
Android 无法识别 decodeResource 或 decodeFile 行中的 R.drawable.green_dragon。我也尝试过刷新和清理应用程序。都没有帮助。当我查找图像属性时,类型为文件,路径为 .jpg。
预先感谢您的帮助。
最佳答案
只能使用 R
对象引用应用程序打包的资源。
您下载的任何内容都必须保存到数据库或文件中。我假设您已经下载并保存了文件。此时,您需要一个具有 jpg 的 Path
的字符串或 jpg 的 FileDescriptor
。
一旦你有你可以加载位图使用:
Bitmap bmp = BitmapFactory.decodeFile( PathToFileString );
或
Bitmap bmp = BitmapFactory.decodeFileDescriptor( fileDescriptorObject );
关于android - 从 jpeg 创建位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056709/