android:保存图像时出现空指针
我正在尝试拍摄一张图像,将其优化为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您正在启动本机相机应用程序,并且此代码可能全部来自 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 returnedIntent
. This would mean the image at the path you expect is null and you have to extract the image from the intent usinggetData()
sometimes.最后一行中的空指针异常是由于最后一行中使用的两个变量为空
检查 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