【问题标题】:Create Bitmap from jpeg从 jpeg 创建位图
【发布时间】:2012-01-29 20:57:33
【问题描述】:

在 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。

提前感谢您的帮助。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    只有与应用程序一起打包的资源才能使用R 对象引用。

    您下载的任何内容都必须保存到数据库或文件中。我假设您已经下载并保存了文件。此时,您要么需要具有 jpg 的 Path 的字符串,要么需要具有 jpg 的 FileDescriptor 的字符串。

    一旦你有任何一个,你可以使用加载位图:

    Bitmap bmp = BitmapFactory.decodeFile( PathToFileString ); 
    

    Bitmap bmp = BitmapFactory.decodeFileDescriptor( fileDescriptorObject );
    

    【讨论】:

    • 一切就绪。将图像复制到可绘制文件夹时工作。谢谢!
    【解决方案2】:

    也许drawBitmapsrcRect 不应该为空,但是这个:

    Rect srcRect = new Rect(0, 0, im.getWidth(), im.getHeight());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2011-04-10
      • 1970-01-01
      • 2014-08-16
      • 2014-05-29
      相关资源
      最近更新 更多