2013-07-11 90 views
0

所以我正在做的是从另一个应用程序接收数据作为意图。我得到的图像不是尝试,直到你达到你的输入流的末尾,将其保存保存图像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")))); 

} 
+0

你的问题不清楚。你究竟在问什么? – veritas

+0

我希望能够将Uri作为图片保存在我的画廊中 –

回答

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