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 pixel开发者_Go百科s 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
I think you should close fos
stream in the createPicture()
method.
BitmapFactory.decode*()
methods return null
in case of error. So it's better to check for null
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.
精彩评论