0

在我的项目中,我有一张图片,我通过程序提取像素,处理这些像素,然后将其保存在我的包中。我保存它的方式是:

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 指示图像的结果为 null 并且程序到达 getWidth 时崩溃。

这些写作和阅读方式正确吗?能否通过模拟器进入目录查看这张图片是否创建?

谢谢

4

1 回答 1

0

我认为你应该在方法中关闭fos流。createPicture()

BitmapFactory.decode*()方法null在错误的情况下返回。所以最好null解码后检查。

据我所知,您只能将 Android 内部存储中的文件保存在模拟器或有根设备上。这可以使用 Eclipse 的 DDMS Perspective 或 Android SDK 工具链中的 ddms 实用程序来完成。只需单击设备-> 文件资源管理器并从设备中提取文件。

如果您想从非 root 设备获取文件,最好将其保存到外部存储(sdcard)。在这种情况下,您可以将设备连接到外部硬盘驱动器等计算机。

于 2010-12-03T14:02:13.133 回答