我将一个图像文件读入一个字节的array.This字节数组中,我必须再次将一个图像文件作为一个图像文件保存到sdcard.To中读取该文件,我使用了下面的代码:
public void readimage()
{
InputStream ins_image = getResources().openRawResource(R.drawable.btn_cancel);
outputStream=new ByteArrayOutputStream();
try
{
ins_image.available();
} catch (IOException e) { e.printStackTrace(); }
try
{
Log.e( "Size of image", ""+ins_image.available());
} catch (IOException e) {e.printStackTrace();}
int size = 0;
byte[] buffer_image = new byte[200000];
try {
while((size=ins_image.read(buffer_image,0,200000))>=0)
{
outputStream.write(buffer_image,0,size);
}
} catch (IOException e) { e.printStackTrace(); }
int length_of_image= outputStream.toByteArray().length;
byte_image=outputStream.toByteArray();
Log.e("Size of image",""+length_of_image);
} 和下面的代码来保存文件:
public void saveimage_fromarray()
{
File photo=new File(Environment.getExternalStorageDirectory(), "photo.png");
if (photo.exists())
{
photo.delete();
}
try
{
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(byte_image[0]);
fos.close();
}
catch (java.io.IOException e)
}然而,文件正在被保存,但它没有显示anything.Can,有人能告诉我为什么会这样吗?
发布于 2013-03-29 20:59:15
看起来你只写了图像的一个字节。
fos.write(byte_image[0]);请比较源文件、字节缓冲区、数组和输出文件的大小。
发布于 2013-03-29 21:05:36
设置要获取的图像的大小,而不是0。
fos.write(byte_image[0]);发布于 2013-03-29 21:01:54
为什么不简化所有的事情,只调用this method
bmp.compress(Bitmap.CompressFormat.PNG, 100, <pass here a valid file outputstream>);https://stackoverflow.com/questions/15703657
复制相似问题