Android: Copy and paste of an image do not work!
I want to copy a picture from asstets folder and paste it in my package, just for test. but final image doesn't show anything. when I want to open picture with paint, it says that "This is not a valid bitmap file".
In my program, I start to read original image in this way:
private void copyImage()
{
AssetManager am = getResources().getAssets();
try{
image = BitmapFactory.decodeStream(am.open("tasnim.png"));
imWidth = image.getWidth();
imHeight = image.getHeight();
}
catch(IOException e){
e.printStackTrace();
System.out.println("Error accoured!");
}
}
next, I'll get pixels of image or extract pixels, and save pixels in array of integer (rgbstream).
private void getPixelsOfImage()
{
rgbStream = new int[imWidth * imHeight];
image.getPixels(rgbStream, 0, imWidth, 0, 0, imWidth, imHeight);
}
finally, I want to save it in my package,
private void createPicture()
{
contextPath = context.getFilesDir().getAbsolutePath();
String path = contextPath + "/" + picName;
try{
FileOutputStream fos = new FileOutputStream(path);
DataOutputStream dos = new DataOutputStream(fos);
for(int i=0; i<rgbStream.length; i++)
dos.writeByte(rgbStream[i]);
dos.flush();
dos.close();
fos.close();
}
catch(IOException e){
System.out.println("IOException : " + e);
}
System.out.println("Picture Created.");
}
Code works fine but result, nothing!!! :( When I check DDMS it creates new file and store all pixels (because it shows the size of this 开发者_JAVA百科file is 13300 and dimension of my original picture is 100*133). when I click "pull a file from the device" I can save it on my desktop. However, when I open it :) nothing.
What you think? is there any problem in my code? Thanks
I don't know what your intent is - do you want to write out a raw image file?
Assuming that you want to write a JPEG or PNG or whatever, you can erase your entire code and do something a lot easier:
Bitmap image = BitmapFactory.decodeStream(am.open("tasnim.png"));
FileOutputStream fos = new FileOutputStream(path);
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
With proper error checking of course.
精彩评论