Java 在Android设备上转换和保存图像文件时出现NullPointerException
我正在尝试将一个图像类型的数据作为jpg文件保存到SD卡,我计划首先将其转换为位图,将位图压缩为jpeg格式,然后使用FileOutputStream将其保存到SD卡 这是我的密码:Java 在Android设备上转换和保存图像文件时出现NullPointerException,java,android,bitmap,fileoutputstream,Java,Android,Bitmap,Fileoutputstream,我正在尝试将一个图像类型的数据作为jpg文件保存到SD卡,我计划首先将其转换为位图,将位图压缩为jpeg格式,然后使用FileOutputStream将其保存到SD卡 这是我的密码: File imagefile = new File(sdCardPath, "image.jpg"); Image image = fromFrame.getImage(); ByteBuffer bbuffer = image.getPlanes()[0].getBuffer(); byte
File imagefile = new File(sdCardPath, "image.jpg");
Image image = fromFrame.getImage();
ByteBuffer bbuffer = image.getPlanes()[0].getBuffer();
byte[] byts = new byte[bbuffer.capacity()];
bbuffer.get(byts);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(byts, 0, byts.length, null);
try{
FileOutputStream out = new FileOutputStream(imagefile);
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
} catch (IOException e) {
Log.v(TAG, "IOExceptionError " + e.toString());
}
它给出了错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat,
int, java.io.OutputStream)' on a null object reference
有什么我遗漏了或弄错了吗?结果是图像文件必须重新组织,而对于Depth16文件,每个像素都有16位数据,因此将其转换为jpg文件的代码是:
Image depthimage = fromFrame.getImage();
int imwidth = depthImage.getWidth();
int imheight = depthImage.getHeight();
Image.Plane plane = depthImage.getPlanes()[0];
ShortBuffer shortDepthBuffer = plane.getBuffer().asShortBuffer();
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "depthImage.jpg");
Bitmap disBitmap = Bitmap.createBitmap(imwidth, imheight, Bitmap.Config.RGB_565);
for (int i = 0; i < imheight; i++) {
for (int j = 0; j < imwidth; j++) {
int index = (i * imwidth + j) ;
shortDepthBuffer.position(index);
short depthSample = shortDepthBuffer.get();
short depthRange = (short) (depthSample & 0x1FFF);
byte value = (byte) depthRange ;
disBitmap.setPixel(j, i, Color.rgb(value, value, value));
}
}
Matrix matrix = new Matrix();
matrix.setRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(disBitmap, 0, 0, imwidth, imheight, matrix, true);
try {
FileOutputStream out = new FileOutputStream(file);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MainActivity.num++;
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
Image depthimage=fromFrame.getImage();
int imwidth=depthImage.getWidth();
int imheight=depthImage.getHeight();
Image.Plane Plane=depthImage.getPlanes()[0];
ShortBuffer shortDepthBuffer=plane.getBuffer().asShortBuffer();
文件sdCardFile=Environment.getExternalStorageDirectory();
File File=新文件(sdCardFile,“depthImage.jpg”);
Bitmap disBitmap=Bitmap.createBitmap(imwidth、imheight、Bitmap.Config.RGB_565);
对于(int i=0;i
我还旋转图片以便于在移动设备上查看可能是由于图像格式造成的,我尝试了Depth16和yuv_420_888,两者都给出了相同的错误。