android - 从 jpeg 创建位图

标签 android android-layout

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

相关文章:

android - 如何测试 Jetpack Compose?

android - 清除EditText中光标位置前的单个字符

android - 如何自动调整 v7.widget.Toolbar 中 setTitle() 文本的大小

android - 如何在单个表格行中添加两个图像

android - 更改 android.support.v7.widget.SearchView 的颜色

android - 如何在适配器 ListView 中添加 onclick 按钮

android - 如何将 NavigationDrawer 放在布局后面?

java - 如何在 android 中备份电池插头上的数据?

android - 在Android中将应用程序字体更改为roboto字体的最简单解决方案

android - 识别要在 ImageView 中显示的图像部分