0

我正在将图像文件读入字节数组。这个字节数组我必须再次作为图像文件保存到 sdcard 上。要读取文件,我使用了以下代码:

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) 
 }

但是文件正在保存,但没有显示任何内容。有人可以告诉我为什么会这样吗?

4

3 回答 3

1

设置您要获取的图像的大小以代替 0。

fos.write(byte_image[0]);
于 2013-03-29T13:05:35.540 回答
0

好像你只写了一个字节的图像。

fos.write(byte_image[0]);

请比较源文件、字节缓冲区、数组和输出文件的大小。

于 2013-03-29T12:59:14.887 回答
0

你为什么不简化一切,只调用这个方法

bmp.compress(Bitmap.CompressFormat.PNG, 100, <pass here a valid file outputstream>);
于 2013-03-29T13:01:53.673 回答