如何在android中保存图像文件?

如何在android中保存图像文件?,android,Android,我正在将图像文件读取到字节数组中。此字节数组必须再次作为图像文件保存到SD卡上。要读取该文件,我使用了以下代码: public void readimage() { InputStream ins_image = getResources().openRawResource(R.drawable.btn_cancel); outputStream=new ByteArrayOutputStream(); try { ins_image.available(); } catch

我正在将图像文件读取到字节数组中。此字节数组必须再次作为图像文件保存到SD卡上。要读取该文件,我使用了以下代码:

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

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

似乎您只写入了图像的一个字节

fos.write(byte_image[0]);

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

为什么不简化一切,只调用:

bmp.compress(Bitmap.CompressFormat.PNG,100,);

将要获取的图像大小设置为0

fos.write(byte_image[0]);

谢谢齐隆尼。我真粗心……我没注意到。
fos.write(byte_image[0]);