Android:解码位图图像时出现问题
在我的项目中,我有一个图片,通过程序,我提取像素,操作这些像素,然后将其保存在我的包中。我保存它的方式是:
private void createPicture()
{
System.out.println("Inside createPicture");
System.out.println(contextPath);
try{
FileOutputStream fos = new FileOutputStream(contextPath + "/" + picName);
DataOutputStream dos = new DataOutputStream(fos);
for(int i=0; i<splitedPixel.length; i++)
dos.writeByte(splitedPixel[i]);
dos.flush();
dos.close();
}
catch(IOException e){
System.out.println("IOException : " + e);
}
System.out.println("Picture Created.");
}
所以,它将我的图片保存在这个目录中
/data/data/my_package_name/files/myPic.bmp
现在,我想读取这张新图片并提取该图片的像素。我用过这个方法:
public Stego(Context context)
{
//Instantiate an ImageView and define its properties
contextPath = context.getFilesDir().getAbsolutePath();
image = BitmapFactory.decodeFile(contextPath + "/" + picName);
System.out.println("Image= " + image);
imWidth = image.getWidth();
imHeight = image.getHeight();
getMaxMessageChars();
System.out.println("Width: " + imWidth);
}
程序在这里崩溃。 logcat 指示图像的结果为空,并且当程序到达 getWidth 时崩溃。
这些书写和阅读的方式正确吗? 可以通过模拟器去该目录查看这张图片是否创建吗?
谢谢
In my project I have a Picture that through program, I extracted pixels, manipulate those pixels and then saved it in my package. The way that I saved it, is:
private void createPicture()
{
System.out.println("Inside createPicture");
System.out.println(contextPath);
try{
FileOutputStream fos = new FileOutputStream(contextPath + "/" + picName);
DataOutputStream dos = new DataOutputStream(fos);
for(int i=0; i<splitedPixel.length; i++)
dos.writeByte(splitedPixel[i]);
dos.flush();
dos.close();
}
catch(IOException e){
System.out.println("IOException : " + e);
}
System.out.println("Picture Created.");
}
so, it saves my picture in this directory
/data/data/my_package_name/files/myPic.bmp
Now, I want to read this new Picture and extract pixels of this picture. I have used this method:
public Stego(Context context)
{
//Instantiate an ImageView and define its properties
contextPath = context.getFilesDir().getAbsolutePath();
image = BitmapFactory.decodeFile(contextPath + "/" + picName);
System.out.println("Image= " + image);
imWidth = image.getWidth();
imHeight = image.getHeight();
getMaxMessageChars();
System.out.println("Width: " + imWidth);
}
Program crashes here. logcat indicates that the result for the image is null and when program reaches to getWidth crashes.
are these ways for writing and reading correct?
Can I go to the directory through emulator to check Whether this picture is created or not?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该在
createPicture()
方法中关闭fos
流。如果发生错误,
BitmapFactory.decode*()
方法将返回null
。因此最好在解码后检查null
。据我所知,您只能将 Android 内部存储中的文件保存在模拟器或 root 设备上。这可以使用 Eclipse 的 DDMS Perspective 或 Android SDK 工具链中的 ddms 实用程序来完成。只需点击设备 -> 文件资源管理器,然后从设备中提取文件。
如果您想从非root设备获取文件,最好将其保存到外部存储(sd卡)。在这种情况下,您可以将设备连接到计算机(例如外部硬盘驱动器)。
I think you should close
fos
stream in thecreatePicture()
method.BitmapFactory.decode*()
methods returnnull
in case of error. So it's better to check fornull
after decoding.As far as I know you can save a file from the Android internal storage only on an emulator or a rooted device. This can be done using Eclipse's DDMS Perspective or ddms utility from Android SDK toolchain. Just click Device->File Explorer and pull your files from a device.
If you want to get a file from a non-rooted device, you'd better save it to the external storage (sdcard). In this case you can connect your device to a computer like external hard drive.