2017-04-11 26 views
2

在我的應用我使用下面的代碼返回輸入流轉換輸入流中的文件的Android

QBContent.downloadFileById(fileId, new QBEntityCallback<InputStream>() { 
@Override 
public void onSuccess(final InputStream inputStream, Bundle params) { 
    long length = params.getLong(Consts.CONTENT_LENGTH_TAG); 
    Log.i(TAG, "content.length: " + length); 

    // use inputStream to download a file 
} 

@Override 
public void onError(QBResponseException errors) { 

} 
}, new QBProgressCallback() { 
@Override 
public void onProgressUpdate(int progress) { 

} 
}); 

現在我想隱蔽輸入蒸汽進入文件,然後想做兩件事情與該文件 1.如何我可以使用意圖 筆記保存到用戶的手機存儲 2.保存它暫時的,顯示的是PDF查看器:返回的文件將在PDF正式

+0

你有沒有找到一個正確的解決方案? –

回答

0

您可以使用下面的代碼保存在InputStream文件。

但是您需要傳遞文件路徑以及要將文件存儲在何處的位置。

InputStream inputStream = null; 
BufferedReader br = null; 

try { 
    // read this file into InputStream 
    inputStream = new FileInputStream("/Users/mkyong/Downloads/file.js"); 
    br = new BufferedReader(new InputStreamReader(inputStream)); 
    StringBuilder sb = new StringBuilder(); 

    String line; 
    while ((line = br.readLine()) != null) { 
     sb.append(line); 
    } 
    System.out.println(sb.toString()); 
    System.out.println("\nDone!"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (inputStream != null) { 
      try { 
       inputStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (br != null) { 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
1

,如果你想在外部或內部存儲來存儲你沒有mentionned,我寫這個例子的內部存儲

BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); 
StringBuilder total = new StringBuilder(); 
String line; 
while ((line = r.readLine()) != null) { 
    total.append(line).append('\n'); 
} 

try { 
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("file.txt", Context.MODE_PRIVATE)); 
    outputStreamWriter.write(total.toString()); 
    outputStreamWriter.close(); 
} 
catch (IOException e) { 
    Log.e("Exception", "File write failed: " + e.toString()); 
} 

不要忘記使用try/catch和關閉哪些需要關閉