2013-07-11 39 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();