从 sdcard 将图像文件读入位图时,为什么会出现 NullPointerException?
如何从 SD 卡将图像文件读入位图?
_path = Environment.getExternalStorageDirectory().getAbsolutePath();
System.out.println("pathhhhhhhhhhhhhhhhhhhh1111111112222222 " + _path);
_path= _path + "/" + "flower2.jpg";
System.out.println("pathhhhhhhhhhhhhhhhhhhh111111111 " + _path);
Bitmap bitmap = BitmapFactory.decodeFile(_path, options );
我收到位图的 NullPointerException 异常。这意味着位图为空。但我有一个图像“.jpg”文件作为“flower2.jpg”存储在SD卡中。有什么问题吗?
How can I read an image file into bitmap from sdcard?
_path = Environment.getExternalStorageDirectory().getAbsolutePath();
System.out.println("pathhhhhhhhhhhhhhhhhhhh1111111112222222 " + _path);
_path= _path + "/" + "flower2.jpg";
System.out.println("pathhhhhhhhhhhhhhhhhhhh111111111 " + _path);
Bitmap bitmap = BitmapFactory.decodeFile(_path, options );
I am getting a NullPointerException for bitmap. It means that the bitmap is null. But I have an image ".jpg" file stored in sdcard as "flower2.jpg". What's the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MediaStore API 可能会丢弃 Alpha 通道(即解码为 RGB565)。如果你有文件路径,只需直接使用 BitmapFactory,但告诉它使用保留 alpha 的格式:
或
http://mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html
The MediaStore API is probably throwing away the alpha channel (i.e. decoding to RGB565). If you have a file path, just use BitmapFactory directly, but tell it to use a format that preserves alpha:
or
http://mihaifonoage.blogspot.com/2009/09/displaying-images-from-sd-card-in.html
有用:
It works:
试试这个代码:
Try this code:
我编写了以下代码,将图像从 sdcard 转换为 Base64 编码字符串,以作为 JSON 对象发送。它效果很好:
I wrote the following code to convert an image from sdcard to a Base64 encoded string to send as a JSON object.And it works great: