0

所以我正在做的是从另一个应用程序接收数据作为意图。我正在获取图像而不是尝试保存它

  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"))));

}
4

1 回答 1

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();
于 2013-07-11T03:23:26.370 回答