Android BitmapFactory.decodeFile(path)始终返回null

康拉多

我看到有很多与此问题相关的解决方案,但是都没有解决我的问题。

我尝试使用BitmapFactory.decodeFile(path)函数将图像从png文件读取到位图(图像很小〜16px,所以我没有OutOfMemoryException选项)。

我的文件层次结构尽可能简单:
-java
--example.com.app
--- MainActivity.class
-res
--drawable
--- array.png

我从MainActivity启动应用程序,然后单击按钮,尝试将png图像读取到位图。我尝试通过以下方式将图像读取到位图:

  1. String path = Uri.parse("android.resource://"+R.class.getPackage().getName() +"/drawable/arrow.png").toString(); Bitmap bitmap = BitmapFactory.decodeFile(path);

  2. String path = Uri.parse("android.resource://drawable/arrow.png").toString(); Bitmap bitmap = BitmapFactory.decodeFile(path);

每次位图为空。您知道哪里可能有问题吗?


Android API 27

哈伦·哈耶姆

或使用简单的方法 BitmapFactory

BitmapFactory.decodeResource(getResources(), R.id.arrow);

或者,您可以使用context.getDrawable(R.drawable.arrow)which将返回drawable准备使用的状态。在这里阅读更多关于此的信息

如果您仍想使用该路径,请在此处java.nio.file.Paths(folder1, folder2.. .. .)阅读有关此内容的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

BitmapFactory.decodeFile() 返回 null xamarin.android

为什么Android BitmapFactory.decodeFile()需要4倍的内存?

Android中的BitmapFactory.decodeFile和ImageView奇怪的东西

来自Assets的BitmapFactory.decodeStream在Android 7上返回null

Android Studio Kotlin:BitmapFactory.decodeStream()返回null

BitmapFactory.decodeFile() 在某些设备中返回 null

BitmapFactory.decodeFile返回null,并将inJustDecodeBounds设置为false

bitmapFactory.decodefile提供null作为输出

Android BitmapFactory与BufferedInputStream

BitmapFactory.decodeResource在Android 2.2上为null

BitmapFactory.decodeFile()中的OutOfMemoryError

如何模拟BitmapFactory:方法decodeFile

Android无法解码流:bitmapFactory.decodeFile()的java.io.FileNotFoundException

在Android中,BitmapFactory.decodeByteArray返回带有inJustDecoteBounds = true的null

安卓; BitmapFactory.decodeFile(file)返回null,已知文件存在

Android:BitmapFactory解码速度缓慢

BitmapFactory.decodeResource在Android 2.2中返回可变的位图,在Android 1.6中返回不可变的位图

BitmapFactory 返回空位图

为什么BitmapFactory.decodeByteArray返回null?

尽管存在图像,但BitmapFactory返回null

如何使用 Android BitmapFactory.Options.inBitmap?

从文件路径设置ImageView的图像。BitmapFactory.decodeFile,BitmapFactory.decodeStream或Drawable.fromFile?

除非我等待接受照片,否则BitmapFactory.decodeFile为null

BitmapFactory.decodeFile()不AdobeImageIntent.EXTRA_OUTPUT_URI工作

是否可以使BitmapFactory.decodeFile()尊重EXIF?

设置选项后,BitmapFactory.decodeStream返回null

为什么我的BitmapFactory.decodeByteArray返回null?

BitmapFactory.decodeStream返回null(jar文件中的图像)

Android findViewById始终返回null