android:保存图像时出现空指针

发布于 2024-10-18 05:05:41 字数 1058 浏览 2 评论 0原文

我正在尝试拍摄一张图像,将其优化为 65% jpeg 并将其保存到手机存储中。 下面的代码在我的连接上运行良好,但在最后一行返回空指针异常。

知道我做错了什么吗? 谢谢

// image passed in from camera
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath, opts);

// create output
FileOutputStream fileOutputStream = null;

// create filename & directory
String nameFile = "ms" + String.valueOf(System.currentTimeMillis())+ ".jpg";
String directory = "/DCIM/MySeats/";

// try creating the directories if they don't already exist         
File file = new File(Environment.getExternalStorageDirectory(),directory);
file.mkdirs();

// prep output
fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+ directory + nameFile);
optimizedImagePath = Environment.getExternalStorageDirectory().toString()+ directory + nameFile;

// write file through a buffered stream
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

// This line causes a null pointer exception on some phones?
// What am I missing?
bm.compress(CompressFormat.JPEG, 65, bos);

I'm trying to take an image, optimize it as a 65% jpeg and save it to the phones storage.
The code below is working fine for me on my nexus one, but is returning a null pointer exception on the last line.

Any idea what I'm doing wrong?
Thanks

// image passed in from camera
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath, opts);

// create output
FileOutputStream fileOutputStream = null;

// create filename & directory
String nameFile = "ms" + String.valueOf(System.currentTimeMillis())+ ".jpg";
String directory = "/DCIM/MySeats/";

// try creating the directories if they don't already exist         
File file = new File(Environment.getExternalStorageDirectory(),directory);
file.mkdirs();

// prep output
fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+ directory + nameFile);
optimizedImagePath = Environment.getExternalStorageDirectory().toString()+ directory + nameFile;

// write file through a buffered stream
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

// This line causes a null pointer exception on some phones?
// What am I missing?
bm.compress(CompressFormat.JPEG, 65, bos);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难如初 2024-10-25 05:05:42

我假设您正在启动本机相机应用程序,并且此代码可能全部来自 onActivityResult() 方法。
您的问题可能是,一些具有特定于运营商版本的手机会忽略 MediaStore.EXTRA_OUTPUT 参数,而仅在返回的 Intent 中返回图像。这意味着您期望的路径中的图像为空,有时您必须使用 getData() 从意图中提取图像。

I am going to assume you are launching the native camera application and this code is all probably from the onActivityResult() method.
Your problem could be that a couple of phones with the carrier specific builds disregard the MediaStore.EXTRA_OUTPUT parameter and just return the image in the returned Intent. This would mean the image at the path you expect is null and you have to extract the image from the intent using getData() sometimes.

云朵有点甜 2024-10-25 05:05:41

最后一行中的空指针异常是由于最后一行中使用的两个变量为空

检查 bm != null
检查 bos!= null

Null pointer exception in the last line is due to the two variable that is used in the last line is null

check for bm != null
check for bos!= null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文