将图像保存为图像

时间:2013-07-11 03:17:38

标签: java android uri

所以我正在做的是从另一个应用程序接收数据作为意图。我得到的图像比试图保存它

  void savefile(Uri sourceuri)
{
    String sourceFilename= sourceuri.getPath();
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PhotoSaver");


    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
        bis = new BufferedInputStream(new FileInputStream(sourceFilename));
        bos = new BufferedOutputStream(new FileOutputStream(mediaStorageDir, false));
        byte[] buf = new byte[1024];
        bis.read(buf);
        do {
            bos.write(buf);
        } while(bis.read(buf) != -1);
    } catch (IOException e) {

    } finally {
        try {
            if (bis != null) bis.close();
            if (bos != null) bos.close();
        } catch (IOException e) {

        }
    }

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "PhotoSaver"))));

}

1 个答案:

答案 0 :(得分:1)

您应该阅读直到到达输入流的末尾并最终刷新输出流。像这样:

     // the file is read to the end
     while ((value = bis.read()) != -1) {
        bos.write(value);
     }

     // invokes flush to force bytes to be written out to baos
     bos.flush();